Home > Backend Development > C++ > body text

How do I print boolean values in C and control their display format?

Linda Hamilton
Release: 2024-10-27 18:45:31
Original
460 people have browsed it

How do I print boolean values in C   and control their display format?

Printing Boolean Values in C

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!