Home C++ Iostream Functions noshowbase C++ noshowbase Manipulator Definition In C++, noshowbase turns off the showbase setting. After it, hex numbers print without 0x and octal numbers print without the leading 0.
This is the normal state of a stream. Use noshowbase to undo an earlier showbase, or to be sure that no prefix appears. 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::noshowbase into the stream.
std::cout << std::hex << std::showbase << 255; // 0xff
std::cout << std::noshowbase << 255; // ff Call What it does std::cout << std::noshowbaseTurns the prefix off. std::cout << std::hex << std::noshowbase << 255Prints ff. std::cout << std::oct << std::noshowbase << 8Prints 10.
Turning the prefix off is useful when you add your own text in front, such as # for a colour.
Library declarations ios_base& noshowbase(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << noshowbase. 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 noshowbase.
C++ Examples 1. Remove 0x 2. Octal 3. Colour code Example 1 — Remove the 0x Prefix Turn the prefix on, print, turn it off, and print again.
#include <iostream>
int main() {
std::cout << std::hex << std::showbase << 255 << '\n';
std::cout << std::noshowbase << 255 << '\n';
return 0;
} How It Works With hex and showbase, the first line is 0xff. noshowbase turns only the prefix off.The base is still 16, so the second line prints ff. Example 2 — noshowbase with Octal Numbers The leading zero of octal numbers disappears too.
#include <iostream>
int main() {
std::cout << std::oct << std::showbase << 8 << ' ';
std::cout << std::noshowbase << 8 << '\n';
return 0;
} How It Works In octal, 8 is written 10. With showbase a leading zero is added: 010. noshowbase comes next in the same chain and removes the prefix.The second 8 prints as 10. The reader must know that the numbers are in base 8. Example 3 — Build a Colour Code with Your Own Prefix Print three hex bytes after a #. The automatic 0x prefix would be wrong here.
#include <iostream>
int main() {
std::cout << '#' << std::noshowbase << std::hex
<< 171 << 205 << 239 << '\n';
return 0;
} How It Works 171, 205, and 239 are the values of the three colour parts.std::hex prints each of them as two hex digits: ab, cd, and ef.noshowbase makes sure that no 0x is printed in front of each part, so the result is the clean text #abcdef.Frequently Asked Questions What does noshowbase do in C++? It turns off the base prefix, so hex numbers print without 0x and octal numbers without a leading 0.
Is noshowbase the default? Yes. A new stream does not show the prefix, so noshowbase is needed only to undo showbase.
Does noshowbase change hex back to decimal? No. It only removes the prefix. Use std::dec to go back to decimal.
Why would I want to hide the prefix? When you add your own prefix, such as # for a colour, or when you print a plain hex string like a hash or an id.
What is the same as noshowbase? cout.unsetf(std::ios::showbase) has the same effect.
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.
9 people found this page helpful
Helpful Share Copy link Suggestion