In C++, boolalpha is a manipulator that changes how a bool is shown. Normally cout << true prints 1. After boolalpha it prints the word true.
It also works for input: with boolalpha on, cin >> flag accepts the words true and false. The setting stays until you turn it off with noboolalpha. 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::boolalpha into the stream before the bool values.
C++
std::cout << std::boolalpha << true; // true
std::cin >> std::boolalpha >> flag; // reads the word true or false
Call
What it does
std::cout << std::boolalpha
Prints bools as words from now on.
std::cout << std::boolalpha << (5 > 3)
Prints true for a comparison result.
std::cin >> std::boolalpha >> flag
Reads the words true or false into a bool.
Only bool values are affected. An int such as 1 is still printed as 1.
Library declarations
C++
ios_base& boolalpha(ios_base& str);
Parameters
Parameter
Description
str
The stream. It is passed in for you when you write cout << boolalpha. 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 boolalpha.
C++ Examples
Example 1 — Print true and false as Words
Compare the output before and after boolalpha.
C++
#include <iostream>
int main() {
bool yes = true;
bool no = false;
std::cout << yes << ' ' << no << '\n';
std::cout << std::boolalpha;
std::cout << yes << ' ' << no << '\n';
return 0;
}
Output
1 0
true false
How It Works
By default a bool is printed as a number: 1 for true and 0 for false.
std::cout << std::boolalpha prints nothing, but it switches the stream to word mode.
The second line prints the same two variables as the words true and false.
Example 2 — Show the Result of a Comparison
A comparison gives a bool, so boolalpha makes the answer easy to read.