c++ - (C++) 为什么 '||' 逻辑运算符返回 1?

标签 c++

我是 C++ 初学者。为什么当我写出这段代码时,它返回“1”?

cout << (false||(!false));

它写出“1”,相当于“true”。

为什么它返回 true 而不是 false?它如何判断该陈述是否正确?

最佳答案

How does it decide whether the statement is true or not?

bool 运算符遵循 bool 代数规则。

! 运算符(not)对应于逻辑非。如果操作数为 true,则结果为 false;如果操作数为 false,则结果为 true。

||(包含或)运算符对应于逻辑(包含)析取。仅当两个操作数都为假时,结果才为假。否则结果为真。

输出为 1,因为当您插入 true bool 时,标准输出流会生成字符 1(除非设置了 std::ios_base::boolalpha 格式标志)。

关于c++ - (C++) 为什么 '||' 逻辑运算符返回 1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58273815/

相关文章:

c++ - 英特尔 PIN 例程地址检索 : Linux vs. Windows

c++ - 如何获取具有给定类名的未知类的对象

c++ - 为什么在 Windows 和 Mac OS 中运行 UnionFind 时会存在大量运行时差异?

c++ - 使用模板参数作为模板参数

c++ - Visual Studio 代码语法高亮显示错误但编译

c++ - 在另一个线程中绘制 OpenGL

c++ - 有状态仿函数和 STL : Undefined behaviour

c++ - Dev cpp 中没有错误窗口

python - 使用命令行参数在 C++ 中运行 python .py 文件

c++ - 如何将 vector 从成员函数传递给同一类中的另一个成员函数?