Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean stream output representation in C++

I have c++ code

int main()
{
   int a = 5, b=5;
   bool c;
   c = a == b;
   cout << c << endl;
   return 0;
}

Output is 1

anyone please explain me how come output is 1 and why it is not true?

like image 368
Sasikumar Murugesan Avatar asked Sep 03 '25 02:09

Sasikumar Murugesan


1 Answers

Because that's how std::ostream::operator<< formats bool values by default. It outputs a 1 for true, and a 0 for false. If you want to print the text "true" or "false", you can use the boolalpha io manipulator:

std::cout << std::boolalpha << c;
like image 143
Benjamin Lindley Avatar answered Sep 04 '25 14:09

Benjamin Lindley