What is std::boolalpha in C++?

The std::boolalpha feature is a manipulator in C++ that’s used with the std::cout stream (and other output streams) to control the formatting of boolean values. In simple terms, it allows us to display boolean values as words (e.g., “true” or “false”) rather than as numeric values (11 for true and 00 for false).

By default, when we output a boolean value to the console, C++ will display it as either 11 for true or 00 for false. This is suitable in many cases, but sometimes it’s more human-readable and user-friendly to see the words “true” and “false.”

How to use std::boolalpha

Using std::boolalpha is quite straightforward. We just need to include it in our code before we output a boolean value to the console. Let’s look at an example:

#include <iostream>
int main() {
bool myBool = true;
// Use std::boolalpha to format the output
std::cout << std::boolalpha << "My boolean value is: " << myBool << std::endl;
return 0;
}
Example code of using std::boolalpha

In the example above, we first include the necessary header files and create a boolean variable myBool, which is set to true. Then, we use std::boolalpha before outputting the value. The output will be “My boolean value is: true” instead of “My boolean value is: 1”.

Therefore, std::boolalpha is a simple yet powerful thing in C++ that allows us to control how boolean values are displayed. By using it, we can make our code more user-friendly and improve the readability of our program’s output. Therefore, remember to use boolalpha if when we need words and not to use it when we need numbers for boolean values. It’s a handy feature that can enhance your C++ programming experience.

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved