表达式c = a> 2 + b!= 6的输出是什么?

时间:2020-05-24 16:41:29

标签: c operator-precedence evaluation

最近我遇到了这个程序。

#include <stdio.h>

int main() {
    int a = 10, b = 20, c;
    c = a > 2 + b != 6;
    printf("%d", c);
}

1输出背后的逻辑是什么?

1 个答案:

答案 0 :(得分:7)

这取决于precedence of the operators

+的优先级高于>,而>的优先级高于!=

a > 2 + b != 6

被评估为:

((a > (2 + b)) != 6)

或更具体:

((10 > (2 + 20)) != 6)

其中(10 > (20 + 2))的值为0,因为10不大于22

因此表达式展开为:

(0 != 6)

其值为1,因为0不等于6-> (0 != 6) == 1

相关问题