The output of cout << 1 && 0; [closed]

一曲冷凌霜 提交于 2019-11-26 23:19:09

问题


I don't understand why the below code prints 1.

1 && 0 is not the same as true && false -> false?

Why doesn't this print 0?

#include <iostream>

using namespace std;

int main(){
    cout << 1 && 0;
    return 0;
}

回答1:


It's all about Operator Precedence.

The Overloaded Bitwise Left Shift Operator operator<<(std::basic_ostream) has a higher priority than the Logical AND Operator &&.

#include <iostream>
int main() {
    std::cout << (1 && 0);
    return 0;
}

If you are not 146% sure about the priority of an operator, do not hesitate to use brackets. Most modern IDEs will tell you if you don't need to use them.



来源:https://stackoverflow.com/questions/49195740/the-output-of-cout-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!