C++ setbase() Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iomanip

Definition

In C++, setbase(n) sets the number base of a stream. The allowed values are 8 (octal), 10 (decimal), and 16 (hexadecimal). It is the same as oct, dec, and hex.

The advantage is that the base can be a variable, so one line of code works for all three bases. Include <iomanip> to use it.

This is a manipulator with an argument. You write it inside a stream chain, like std::cout << setbase(...), and it changes how the stream treats the data. It lives in <iomanip>, so remember to include that header.

Syntax

Put std::setbase(n) into the stream before the numbers.

C++
#include <iomanip>

std::cout << std::setbase(16) << 255;   // ff
std::cout << std::setbase(8) << 64;     // 100
CallWhat it does
std::cout << std::setbase(16) << 255Prints ff.
std::cout << std::setbase(8) << 64Prints 100.
std::cout << std::setbase(base) << nUses a base that is stored in a variable.

For a fixed base, hex, oct, and dec are shorter. Use setbase when the base is chosen at run time.

Library declarations
C++
/* unspecified */ setbase(int base);

Parameters

ParameterDescription
baseRequired. 8, 10, or 16. Any other value clears the base flags, and output then looks decimal.

Return Value

Return ValueDescription
Unspecified typeA small helper object that sets the number base when it is sent into a stream. You never name its type. Just write setbase(base) inside the chain.

C++ Examples

Example 1 — Print in Hex with setbase

The same as hex, but with a number.

C++
#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::setbase(16) << 255 << '\n';
    std::cout << std::setbase(10) << 255 << '\n';
    return 0;
}

How It Works

  1. setbase(16) sets the base to hexadecimal, so 255 prints as ff.
  2. setbase(10) goes back to decimal.
  3. The second line prints the same value as 255.

Example 2 — One Line for Three Bases

Put the base in a variable and loop over it.

C++
#include <iomanip>
#include <iostream>

int main() {
    int bases[] = {8, 10, 16};

    for (int b : bases) {
        std::cout << "base " << std::dec << b << ": "
                  << std::setbase(b) << 64 << '\n';
    }
    return 0;
}

How It Works

  1. The array holds the three allowed bases.
  2. std::setbase(b) takes the base from the loop variable, so one statement works for all three.
  3. std::dec is put in first so that the base number itself prints in decimal every time. 64 is 100 in octal, 64 in decimal, and 40 in hex.

Example 3 — A Base That Is Not Supported

Binary is not possible. The number simply prints as decimal.

C++
#include <iomanip>
#include <iostream>

int main() {
    std::cout << "setbase(2) prints: " << std::setbase(2) << 5 << '\n';
    return 0;
}

How It Works

  1. setbase(2) is not one of 8, 10, or 16, so it does not turn on binary.
  2. The stream is left with no base flag, and output with no flag is printed as decimal.
  3. The number 5 is printed as 5 and not as 101. For binary output use std::bitset.

Frequently Asked Questions

It sets the number base of a stream to 8, 10, or 16, like oct, dec, and hex.
They give the same result. setbase(16) takes the base as a number, so it can come from a variable.
No. Only 8, 10, and 16 are supported. Use std::bitset for binary.
Yes. Include <iomanip> to use it.
The base flags are cleared. Output then shows decimal numbers, and input accepts a 0 or 0x prefix to choose the base.

Next: setfill()

Choose the padding character used with setw.

setfill() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful