J. Doe J. Doe - 1 month ago 16
C Question

Logical expressions in C

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);

Answer

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.