我不明白为什么下面的代码会打印 1
。
1 && 0
和 true && false
不一样 -> false
?
为什么不打印 0
?
#include <iostream>
using namespace std;
int main(){
cout << 1 && 0;
return 0;
}
最佳答案
一切都是关于 Operator Precedence .
重载的按位左移运算符 operator<<(std::basic_ostream)
具有比逻辑与运算符 &&
更高的优先级.
#include <iostream>
int main() {
std::cout << (1 && 0);
return 0;
}
如果您不能 146% 确定运算符的优先级,请不要犹豫使用括号。大多数现代 IDE 会告诉您是否不需要使用它们。
关于c++ - cout << 1 && 0; 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49195740/