Home >Backend Development >C++ >How do I print boolean values in C and control their display format?
When printing boolean values (bool) in C using standard output streams, you may wonder what specific result appears.
The behavior is controlled by the boolalpha flag, which determines the display format of booleans. When boolalpha is false, the stream outputs "0" for false and "1" for true. When boolalpha is true, it outputs "false" for false and "true" for true.
Here's an example:
<code class="cpp">#include <iostream> #include <iomanip> int main() { std::cout << false << "\n"; // Outputs "0" std::cout << std::boolalpha; // Sets boolalpha to true std::cout << false << "\n"; // Outputs "false" return 0; }</code>
The boolalpha manipulator is used to set the boolalpha flag. Additionally, the actual display word (e.g., "false" or "faux") is localized based on the locale of the output stream.
To display booleans in a specific localized format, you can imbue the stream with a suitable locale or create a custom numpunct facet for localized display handling.
The above is the detailed content of How do I print boolean values in C and control their display format?. For more information, please follow other related articles on the PHP Chinese website!