Home C++ Iostream Functions showbase C++ showbase Manipulator 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.
std::cout << std::hex << std::showbase << 255; // 0xff
std::cout << std::oct << std::showbase << 8; // 010 Call What 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 ios_base& showbase(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << showbase. You never write this argument yourself.
Return Value Return Value Description ios_base&A reference to the same stream, so you can keep chaining << or >> after showbase.
C++ Examples 1. Hex prefix 2. Octal prefix 3. With uppercase Example 1 — Show 0x in Front of a Hex Number Print the same number with and without showbase.
#include <iostream>
int main() {
std::cout << std::hex << 255 << '\n';
std::cout << std::showbase << 255 << '\n';
return 0;
} How It Works std::hex switches the stream to base 16, so 255 prints as ff.std::showbase turns the prefix on. It stays in the chain, and hex is still active.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.
#include <iostream>
int main() {
std::cout << std::oct << std::showbase;
std::cout << 8 << ' ' << 64 << '\n';
return 0;
} How It Works std::oct writes numbers in base 8. The number 8 is 10 in octal.showbase adds a 0 in front, so it prints as 010.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.
#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 With hex and showbase, the first line is 0xff. uppercase changes the letters and the prefix letter, so the second line is 0XFF.std::dec goes back to base 10. Decimal numbers have no prefix, so showbase does nothing and 255 is printed.Frequently Asked Questions What does showbase do in C++? It prints a prefix for the number base: 0x for hexadecimal numbers and a leading 0 for octal numbers.
Why does hex not print 0x by default? The prefix is optional. Add std::showbase to see it, or write the "0x" text yourself.
Does showbase work with decimal numbers? It has no effect on decimal numbers, because decimal has no prefix.
How do I print 0XFF with capital letters? Use std::hex, std::showbase, and std::uppercase together.
How do I turn showbase off? Use std::noshowbase, or cout.unsetf(std::ios::showbase).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion