Search code examples
cincrementboolean-expressionside-effects

Boolean expression with integer increments gives unexpected results


    #include <stdio.h>
    int main()
    {
       int x, y, z; 
       x=y=z=1; 
       z = ++x || ++y && ++z; 
       printf("%d, %d, %d", x, y,z);
       return 0;
    }

I am getting output 2, 1, 1. I need and explanation why Y is 1 in output?


Solution

  • The || operator is short-circuiting; the right operand is not evaluated (and side effects never executed, i.e. the variables never incremented) if the left operand evaluates to != 0.

    (Remember that "multiplication comes before addition", or in boolean logic AND comes before OR. Because of the operator precedences the expression becomes, fully bracketed, (++x) || ((++y) && (++z)); in other words, everything to the right of || is the OR's ignored right hand side.)

    The evaluation of the left operand ++x increments x to 2. z is assigned the boolean value of the result of || which is 1, as it was all along.


    As an aside, gcc warned and I think it is true that the statement is also undefined behavior. The C 11 standard draft n1570 says in 6.5/2:

    If a side effect on a scalar object is unsequenced relative to [...] a value computation using the value of the same scalar object, the behavior is undefined.

    The standard gives the example of i = ++i + 1; for this. In your right hand side expression z plays that role. The side effect is the increment, the value computation is the && sub-expression.


    1 It is possible (i.e. I'm not sure) that it is defined as long as the right hand side is never evaluated. In that case the compiler would be allowed to assume that ++x is never 0 and omit the test. The generated code for the complicated boolean expression would just boil down to an increment of x.