C程序中的意外输出

时间:2017-05-09 12:14:18

标签: c relational-operators

我运行以下C程序

#include <stdio.h>

int main() {
    int x = 5, y = 6, z = 3, i;
    i = y > x > z;
    printf("%d\n", i);
}

并将输出设为0。 再次,当我运行

 #include <stdio.h>

 int main() {
     int x = 5, y = 6, z = 3, i;
     i = y > x && x > z;
     printf("%d\n", i);
 }

我的输出为1。任何人都可以解释这背后的逻辑吗?

1 个答案:

答案 0 :(得分:1)

$this->registerJs("
    $('#form').on('beforeSubmit', function (e) {
        $('#submitButton').css('disable', 'disable');
        return true;
    });
");

在第一个示例中,i = y > x > z; 运算符从左到右的关联性,因此,首先解析>并给出布尔结果。

y > x

然后,

y > x = 6 > 5 = True

所以,输出1(True) > 3 = False

第二,

0

i = y > x && x > z; 运算符更高优先级然后>运算符。因此,首先解析&&,如果条件 True ,请检查y > x