C++ showbase Manipulator

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, showbase makes a stream print a prefix that tells the number base. Hexadecimal numbers get 0x in front, and octal numbers get a leading 0.

It only matters for hex and oct. Decimal numbers have no prefix, so showbase changes nothing for them. It comes from <ios>, which <iostream> includes.

This is a manipulator. You do not call it on its own. You send it into a stream with << or >>, and it changes how that stream behaves from then on (or does one small job, like ending a line).

Syntax

Put std::showbase into the stream together with a base manipulator.

C++
std::cout << std::hex << std::showbase << 255;   // 0xff
std::cout << std::oct << std::showbase << 8;     // 010
CallWhat it does
std::cout << std::showbaseTurns the prefix on.
std::cout << std::hex << std::showbase << 255Prints 0xff.
std::cout << std::oct << std::showbase << 8Prints 010.

Combine showbase with uppercase to get 0XFF instead of 0xff.

Library declarations
C++
ios_base& showbase(ios_base& str);

Parameters

ParameterDescription
strThe stream. It is passed in for you when you write cout << showbase. You never write this argument yourself.

Return Value

Return ValueDescription
ios_base&A reference to the same stream, so you can keep chaining << or >> after showbase.

C++ Examples

Example 1 — Show 0x in Front of a Hex Number

Print the same number with and without showbase.

C++
#include <iostream>

int main() {
    std::cout << std::hex << 255 << '\n';
    std::cout << std::showbase << 255 << '\n';
    return 0;
}

How It Works

  1. std::hex switches the stream to base 16, so 255 prints as ff.
  2. std::showbase turns the prefix on. It stays in the chain, and hex is still active.
  3. The same number now prints as 0xff, so a reader knows it is hexadecimal.

Example 2 — Show the Leading 0 for Octal

In octal, the prefix is a single zero.

C++
#include <iostream>

int main() {
    std::cout << std::oct << std::showbase;
    std::cout << 8 << ' ' << 64 << '\n';
    return 0;
}

How It Works

  1. std::oct writes numbers in base 8. The number 8 is 10 in octal.
  2. showbase adds a 0 in front, so it prints as 010.
  3. 64 is 100 in octal and prints as 0100. The prefix is added to every number until you turn it off.

Example 3 — showbase Together with uppercase

Uppercase makes both the letters and the x of the prefix capital.

C++
#include <iostream>

int main() {
    std::cout << std::hex << std::showbase;
    std::cout << 255 << '\n';

    std::cout << std::uppercase;
    std::cout << 255 << '\n';
    std::cout << std::dec << 255 << '\n';
    return 0;
}

How It Works

  1. With hex and showbase, the first line is 0xff.
  2. uppercase changes the letters and the prefix letter, so the second line is 0XFF.
  3. std::dec goes back to base 10. Decimal numbers have no prefix, so showbase does nothing and 255 is printed.

Frequently Asked Questions

It prints a prefix for the number base: 0x for hexadecimal numbers and a leading 0 for octal numbers.
The prefix is optional. Add std::showbase to see it, or write the "0x" text yourself.
It has no effect on decimal numbers, because decimal has no prefix.
Use std::hex, std::showbase, and std::uppercase together.
Use std::noshowbase, or cout.unsetf(std::ios::showbase).

Next: showpoint

Always print the decimal point and trailing zeros.

showpoint 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.

15 people found this page helpful