请解释这个[重复]的输出

时间:2022-07-05 17:02:59

This question already has an answer here:

这个问题在这里已有答案:

I was trying c programming and I wrote a small code but I am unable to understand this

我正在尝试编程,我写了一个小代码,但我无法理解这一点

#include<stdio.h>
int main()
{

    int x,y,z,k;
    x=y=z=k=1;

    z=x++||y++&&k++;
    printf("%d %d %d %d\n",x,y,z,k);
}

I was expecting output as 2 1 1 2 because the precedence of && is more than || but the output is 2 1 1 1 please explain.

我期待输出为2 1 1 2,因为&&的优先级大于||但输出是2 1 1 1请解释。

1 个解决方案

#1


5  

C uses short-circuit evaluation, so when x++ is evaluated as true, the remaining expressions are not evaluated, and no increment occurs.

C使用短路评估,因此当x ++被评估为true时,不评估剩余的表达式,并且不会发生增量。

#1


5  

C uses short-circuit evaluation, so when x++ is evaluated as true, the remaining expressions are not evaluated, and no increment occurs.

C使用短路评估,因此当x ++被评估为true时,不评估剩余的表达式,并且不会发生增量。