J. Doe J. Doe - 1 year ago 93
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);

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download