cout的输出<< 1&& 0; [英] The output of cout << 1 && 0;

查看:77
本文介绍了cout的输出<< 1&& 0;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么下面的代码会显示1.

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

1 && 0true && false-> false不同吗?

为什么不打印0?

#include <iostream>

using namespace std;

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

推荐答案

这一切与操作员有关优先级.

重载的按位左移运算符operator<<(std::basic_ostream)的优先级高于逻辑与运算符&&.

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;
}

如果您不确定146%的操作员优先级,请不要犹豫使用方括号.大多数现代的IDE都会告诉您是否不需要使用它们.

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.

这篇关于cout的输出&lt;&lt; 1&amp;&amp; 0;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆