97

I print a bool to an output stream like this:

#include <iostream>

int main()
{
    std::cout << false << std::endl;
}

Does the standard require a specific result on the stream (e.g. 0 for false)?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
user788171
  • 16,753
  • 40
  • 98
  • 125
  • 2
    How should we close this one? – Andy Prowl Apr 11 '13 at 22:33
  • Hint: it's guaranteed to print the same thing on every conforming compiler. – chris Apr 11 '13 at 22:33
  • It would've been easier to type two lines of code... – Roberto Apr 11 '13 at 22:38
  • 14
    Not really. To get the answer, one has to write a brand new C++ code, compile it, and then run it. This is a common question that people will commonly search for. No clear answer exists on the net. I have now done mankind a favor and facilitated the answer for the world to see. This is the point of SO is it not? – user788171 Apr 11 '13 at 22:53
  • 6
    @user788171: Writing a brand new 3-line C++ code, compiling it, and then running it takes less time than posting a question and/or searching for an answer on SO. – Andy Prowl Apr 11 '13 at 22:58
  • 7
    @user788171 Consider adding [ideone](http://www.ideone.com) to your repertoire. – JBentley Apr 11 '13 at 23:08
  • 1
    It would actually be *quicker* to use that than to post a SO question. – JBentley Apr 11 '13 at 23:08
  • 10
    What is the purpose of the first line? – JBentley Apr 11 '13 at 23:11
  • 21
    This is now the first result on google for "cout bool". As such this question was definitly helpful and faster than opening ideone myself. Also the answer told me of the existance of `std::boolalpha` – WorldSEnder Sep 21 '17 at 09:39
  • 5
    voted to reopen, this is certainly a real question even if it is obvious to some – M.M Mar 06 '18 at 01:13
  • @AndyProwl That is wrong. I searched for an answer and found this in under a few seconds. For me to test this in an environment I could be pretty certain with, I'd have to open a new, clean project/file and write out the code myself, then compile. While many people do not have a fresh C environment to test in on hand, most have a web browser on hand. – Kröw Feb 04 '23 at 13:55
  • The fact that this question got closed is detestable of Stackoverflow. There is absolutely nothing wrong with this question. The fact that it has a simple, clear answer, and asks about what the specification dictates, makes it very useful to many. Simple != bad. Programmers of all people should know this. – Kröw Feb 04 '23 at 13:58
  • Related: *[What is the printf format specifier for bool?](https://stackoverflow.com/questions/17307275/what-is-the-printf-format-specifier-for-bool)*. See also [the linked questions](https://stackoverflow.com/questions/linked/17307275?sort=votes). – Peter Mortensen Aug 07 '23 at 20:21

2 Answers2

177

The standard streams have a boolalpha flag that determines what gets displayed -- when it's false, they'll display as 0 and 1. When it's true, they'll display as false and true.

There's also an std::boolalpha manipulator to set the flag, so this:

#include <iostream>
#include <iomanip>

int main() {
    std::cout<<false<<"\n";
    std::cout << std::boolalpha;   
    std::cout<<false<<"\n";
    return 0;
}

...produces output like:

0
false

For what it's worth, the actual word produced when boolalpha is set to true is localized--that is, <locale> has a num_put category that handles numeric conversions, so if you imbue a stream with the right locale, it can/will print out true and false as they're represented in that locale. For example,

#include <iostream>
#include <iomanip>
#include <locale>

int main() {
    std::cout.imbue(std::locale("fr"));

    std::cout << false << "\n";
    std::cout << std::boolalpha;
    std::cout << false << "\n";
    return 0;
}

...and at least in theory (assuming your compiler/standard library accept "fr" as an identifier for "French") it might print out faux instead of false. I should add, however, that real support for this is uneven at best--even the Dinkumware/Microsoft library (usually quite good in this respect) prints false for every language I've checked.

The names that get used are defined in a numpunct facet though, so if you really want them to print out correctly for particular language, you can create a numpunct facet to do that. For example, one that (I believe) is at least reasonably accurate for French would look like this:

#include <array>
#include <string>
#include <locale>
#include <ios>
#include <iostream>

class my_fr : public std::numpunct< char > {
protected:
    char do_decimal_point() const { return ','; }
    char do_thousands_sep() const { return '.'; }
    std::string do_grouping() const { return "\3"; }
    std::string do_truename() const { return "vrai";  }
    std::string do_falsename() const { return "faux"; }
};

int main() {
    std::cout.imbue(std::locale(std::locale(), new my_fr));

    std::cout << false << "\n";
    std::cout << std::boolalpha;
    std::cout << false << "\n";
    return 0;
}

And the result is (as you'd probably expect):

0
faux
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
37

0 will get printed.

As in C++ true refers to 1 and false refers to 0.

In case, you want to print false instead of 0,then you have to sets the boolalpha format flag for the str stream.

When the boolalpha format flag is set, bool values are inserted/extracted by their textual representation: either true or false, instead of integral values.

#include <iostream>
int main()
{
  std::cout << std::boolalpha << false << std::endl;
}

output:

false

IDEONE

Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71