Evaluation Order || and && in c

If the priority && is greater than the value ||, should this code not evaluate -b && ++ c first, and therefore the output should be 1 2 4 11. But here it seems to be a short circuit to give 1 2 5 10. Please help!

int x;
int a=1,b=5,c=10;
x=a++||--b&&++c;
printf("%d %d %d %d\n",x,a,b,c);
return 0;
+5
source share
5 answers

this code should not evaluate -b && & ++ c first

No The priority of the operator does not affect the evaluation order. It just means that

a++||--b&&++c

is equivalent to

a++||(--b&&++c)

therefore it is still a++, which is first evaluated and thus closes the operator.

+14
source

&& , , , ||. ,

a++ || (--b && ++c)

|| , -0. a 1, a++, b , c .

+2

, && , , . ||, , , , , .

+2

.

C, || . ++. TRUE, compiler optimizationa short circuit will ensure that the right side || not evaluated because it will not change the result of the expression.

+1
source

Lazy score .

--b && ++c not rated at all.

0
source

All Articles