In this article, we will look at three ways to print a textual representation of a boolean in C++. When we try to print Boolean values in C++, they’re either printed as 0 or 1 by std::cout, but at times it’s better to see the output in the form of true or false. Imagine reading through millions of lines, it is very easy to miss a 0 in a sea of 1's!
How to Print Boolean in C++?
By default, boolean values in C++ are printed as integers. The value false is represented as 0, while the value true is represented as 1. Consider a scenario as discussed above where we have to read through lines and lines of code and log files while debugging, and it will be very easy to miss a 0 or 1 amid a sea of errors and numbers. So it’s better to have the bool value printed as true/false.
We have 3 methods to achieve the "True/False" or Boolean values as our output in C++. So, let's study them in detail one by one.
1) Modify the printf() by Adding Ternary Statement
We can do slight modifications in the printf() to print true or false. We know that to use printf() we can use the format specifiers like %d, %s, etc. for int, string, and so on. But there isn't one for bool's and for bool, %d works because any integral type shorter than int is promoted to int when passed to printf()'s variadic arguments:
Let's look at an example!
Input:
printf("printf true : %d\n", true); printf("printf false: %d\n", false);
Output:
printf true : 1 printf false: 0
Now, to get true or false ends up on your terminal we have to add a ternary if statement and change the format specifier to %s and, as if it were magic!
Input:
printf("printf if true : %s\n", true ? "true" : "false"); printf("printf if false: %s\n", false ? "true" : "false");
Output:
printf if true : true printf if false: false
Note that this ternary if, also called shorthand if, statement. But I do not have much readability from a programmer's perspective, and readability is a big thing. The issue with this method is that when the source code files become large and complex, it hinders readability.
2) Use std::boolalpha in cout
The standard streams consist of a boolalpha flag which decides what gets output/printed on the screen and this std::boolalpha works with the input and output stream functions which can be found in the header file. Use is simple if you're familiar with std::setprecision or std::setw, this is basically the same.
If std::boolalpha is true, then: it displays the textual form of Boolean values, i.e. true or false.
else, when it is set to false, we get bool output as 0 and 1 only.
It is one of the best ways to print true or false as output! but it has a few drawbacks.
But there are 2 drawbacks to this method:
- We must turn off the boolalpha flag after using it via std::noboolalpha.
- For example, if we want to code in Japanese or some other language, and we do want "hai"(true in Japanese) or "nai"(false in Japanese) printed instead of true or false, or if we want to capitalize the words as True or False, we can’t do any of these using std::boolalpha alone.
Printing a bool without the I/O manipulator active results in just 0/1:
//Welcome to favtutor #include using namespace std; int main() { cout<<"when boolalpha flag is off\n"; cout<<"true: "<<true<<endl; cout<<"false: "<<false<<endl; cout<<"when boolalpha flag is on\n"; cout<<boolalpha<<"true: "<<true<<endl; cout<<boolalpha<<"false: "<<false<<endl; cout<<noboolalpha; }
Output:
when boolalpha flag is off true: 1 false: 0 when boolalpha flag is on true: true false: false
3) Use Custom facet for std::numpunc
Something other than true or false?
Now consider if we want to print something other than true or false or capitalized or localized output. You can define a custom facet for std::numpunct.
A facet is a class that describes the locale feature set associated with a specific cultural aspect. Stream input and output operations using std::numpunct through std::numget and std::numput for parsing the numeric input and formatting the numeric output.
The facet for std::numpunct encapsulates the numeric punctuation preferences. By overriding the functions do_truename() and do_falsename() in std::numpunct you can specify which strings are returned when std::boolalpha is active. After that, you use std::cout.imbue() with a std::locale object to replace the current locale.
After this, with std::locale object along with the use of std::cout.imbue() we can replace the current locale.
Below is an example:
// Welcome to Favtutor #include #include #include //numpunct #include //boolalpha class dutch_bool : public std::numpunct< char > { protected: std::string do_truename() const override { return "Waar"; } std::string do_falsename() const override { return "Onwaar"; } }; int main() { // new is not a memory leak here, the dutch_bool object is // implicitly reference counted and it will be destroyed // when the last std::locale referencing it goes out of scope. std::cout.imbue(std::locale(std::locale(), new dutch_bool)); std::cout << std::boolalpha << "NL boolalpha true : " << true << "\n" << "NL boolalpha false : " << false << "\n" << std::noboolalpha; return 0; }
Output:
NL boolalpha true : Waar NL boolalpha false : Onwaar
Conclusion
In this article, we learn different methods to print the bool in C++. The first two methods are preferable if we want true or false as output but have some drawbacks, whereas the third method is preferred when we want some specialized output instead of just true or false.