Home C++ Iostream Functions noboolalpha C++ noboolalpha Manipulator Definition In C++, noboolalpha is a manipulator that turns the boolalpha setting off. After it, a bool is printed as 1 or 0 again.
This is also the default state of a new stream, so you only need noboolalpha to undo an earlier boolalpha. 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::noboolalpha into the stream after you have used boolalpha.
std::cout << std::boolalpha << true; // true
std::cout << std::noboolalpha << true; // 1 Call What it does std::cout << std::noboolalphaPrints bools as 1 and 0 from now on. std::cout << std::boolalpha << b << std::noboolalphaUses the word form for one value, then switches back. std::cin >> std::noboolalpha >> flagReads 0 or 1 into a bool.
New streams already start with noboolalpha, so you will rarely need it at the start of a program.
Library declarations ios_base& noboolalpha(ios_base& str); Parameters Parameter Description strThe stream. It is passed in for you when you write cout << noboolalpha. 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 noboolalpha.
C++ Examples 1. Switch back 2. One value only 3. Read numbers Example 1 — Turn boolalpha Off Again Print the same value in word mode and then in number mode.
#include <iostream>
int main() {
bool ok = true;
std::cout << std::boolalpha << ok << '\n';
std::cout << std::noboolalpha << ok << '\n';
return 0;
} How It Works std::boolalpha switches to word mode, so the first line shows true.std::noboolalpha switches back in the middle of the chain.The second line prints the same variable, but now as the number 1. Example 2 — Use Words for One Value Only Turn on boolalpha just for one item and turn it off straight after.
#include <iostream>
int main() {
bool a = true;
bool b = false;
std::cout << "a = " << std::boolalpha << a << std::noboolalpha
<< ", b = " << b << '\n';
return 0;
} How It Works a is printed while boolalpha is on, so it appears as true.noboolalpha follows at once and turns the word mode off.b is printed in number mode, so you see 0 instead of false.Example 3 — Read 0 and 1 into Bools noboolalpha is the normal mode for input: digits are accepted.
#include <iostream>
#include <sstream>
int main() {
std::istringstream in("1 0");
bool x = false;
bool y = true;
in >> std::noboolalpha >> x >> y;
std::cout << std::boolalpha;
std::cout << "x = " << x << ", y = " << y << '\n';
return 0;
} How It Works noboolalpha makes >> read a bool from the digits 0 or 1.1 becomes true in x, and 0 becomes false in y, even though y started as true.The output uses boolalpha only to make the results easy to read. Frequently Asked Questions What does noboolalpha do in C++? It turns off boolalpha, so bool values are printed as 1 and 0 again.
Is noboolalpha the default? Yes. A new stream already prints bools as 1 and 0, so noboolalpha only undoes an earlier boolalpha.
What is the difference between noboolalpha and boolalpha? boolalpha prints true and false. noboolalpha prints 1 and 0.
Does noboolalpha change how I read input? Yes. It makes cin expect 0 or 1 for a bool. The words true and false are then not accepted.
Can I use unsetf instead? Yes. cout.unsetf(std::ios::boolalpha) does the same job as cout << std::noboolalpha.
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.
8 people found this page helpful
Helpful Share Copy link Suggestion