在c ++中打印boolean

use*_*792 0 c++ printing boolean cout

#include <iostream>
using namespace std ;
int main()
{
    int a=5, b=4;
    cout<< a==b;
}
Run Code Online (Sandbox Code Playgroud)

为什么我不能打印这段代码.我该如何打印布尔值?

Zon*_*eur 7

使用std :: boolalpha打印为truefalse.并添加括号,请参阅Vaughn Cato答案进行解释.

#include <iostream>
#include <iomanip>
using namespace std ;
int main()
{
    int a=5, b=4;
    cout<< boolalpha << (a==b);
}
Run Code Online (Sandbox Code Playgroud)


Vau*_*ato 7

您正在处理运算符优先级问题:

cout << a==b;
Run Code Online (Sandbox Code Playgroud)

被解释为

(cout << a) == b;
Run Code Online (Sandbox Code Playgroud)

因为<<的优先级高于==.