cout << 1 && 0; 的输出
The output of cout << 1 && 0;
我不明白为什么下面的代码打印 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 都会告诉您。
我不明白为什么下面的代码打印 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 都会告诉您。