c - 当我们任意使用 == 时会发生什么?

标签 c operators logical-operators relational

我尝试在 C 中运行以下代码:

#include <stdio.h>

int main() {
   int a = 10, b = 5, c = 5;
   int d;
   d = b + c == a;
   printf("%d", d);
}

我得到的输出为d = 1。有人可以向我解释一下当我们像这样使用 == 时会发生什么吗?

最佳答案

§6.5.9 (== and !=)-http://c0x.coding-guidelines.com/6.5.9.html

The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.)Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.

因此,这里 b+c 等于 a,因为两者的值都是 10,因此它会产生 1 .

关于c - 当我们任意使用 == 时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31500404/

相关文章:

c - 在 C 中修改函数中的变量

c++ - 有很多读者时使用 pthread_rwlock 的效率

Python(或通用编程)。为什么使用 <> 而不是 != 并且有风险吗?

Java 如何将 "Logical And Operator"分配给一个 "boolean variable"

php - 逻辑运算符 - PHP 中的 'or' 与 '||'(双管道)

java - Java中的运算符优先级和逻辑运算符

python - 使用 Cython 将 Python 编译为 C

c - 指针的内存分配

c++ - 为同一类提供不同比较运算符的最佳策略是什么?

c++ - 使用运算符 << 的日志记录功能