Search code examples
coperator-precedence

Shortcircuit Operator Evaluation order


main()
{
int a,b=0,c=1,d=1;
a=++b&&++c||++d;    
printf("%d %d %d",b,c,d);  //1 2 1
b=0,c=1,d=1;
a=b&&++c||++d;
printf("%d %d %d",b,c,d);  //0 1 2
}

Why second printf gives answer 0 1 2 instead of 0 2 1 ?


Solution

  • Why second printf gives answer 0 1 2 instead of 0 2 1 ?

    && is short-circuiting.

    In

    a=b&&++c||++d;
    

    ++c will not be evaluated if b is 0 which is the case here. Hence c is 1 instead of 2.