Reputation: 3
Why is the result "3: 1 0 0 4", when we have incremented x? Why isn't it "3: 1 1 0 4"?
x=0;y=4;z=3;
printf("3: %d %d %d %d\n", ++x || !y, x&&y, !z, y);
Upvotes: 0
Views: 58
Reputation: 2942
As @Sami mentioned in his comment, the order that the arguments gets evaluated is compiler-specific and probably depends on calling convention. In your case the x && y
argument gets computed first before the ++x || !y
.
Upvotes: 0
Reputation: 153338
printf("3: %d %d %d %d\n", ++x || !y, x&&y, !z, y);
++x
is evaluated before !y
because of the ||
. !y
is only evaluated if ++x
result was 0.
But there is no specified order to ++x || !y
vs. x&&y
evaluation. Code lacks a sequence point.
Upvotes: 3
Reputation: 7308
If you compile this with cc
you'll get the warning:
warning: unsequenced modification and access to 'x' [-Wunsequenced]`
Modifying a variable and accessing it elsewhere within printf
is undefined behavior and will not give a logical result, nor the same result on every compiler.
Upvotes: 1