J. Doe J. Doe - 4 months ago 52
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"?

printf("3: %d %d %d %d\n", ++x || !y, x&&y, !z, y);


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.