Hi I have little confused about logical operator.
According to KNK C chapter 5 exercise 3-4.
int i=1;
int j=1;
int k=1;
printf("%d\n",++i || ++j && ++k);
printf("%d %d %d",i,j,k);
I thought the result is 1\n 2 1 2 due to the short -circuit evaluation like ((++i || ++j) && ++k ).
But the answer is 1\n 2 1 1.
Why does variable k increase?
From C Operator Precedence:
&&||Since
&&has precedence 11 and||has 12, the expression++i || ++j && ++kis equal to this:Left-to-right associativity makes it evaluate
++ifirst, concludes that it'strueand short-circuits so(++j && ++k)will not be evaluated.The result is therefore