int i=-1,j=-1,k=0,l=2,m;
m=l++||i++ && j++ && k++;
printf("%d %d %d %d %d",i,j,k,l,m);
Output of the code is: -1 -1 0 3 -1
My question is why i++
j++
and k++
is not being evaluated even when &&
has a higher priority over ||
?
int i=-1,j=-1,k=0,l=2,m;
m=l++||i++ && j++ && k++;
printf("%d %d %d %d %d",i,j,k,l,m);
Output of the code is: -1 -1 0 3 -1
My question is why i++
j++
and k++
is not being evaluated even when &&
has a higher priority over ||
?
Essentially what's happening is that as l++=3 which is not 0, it evaluates to True (only 0 would be False). Thus, the second part of the expression after || is not evaluated.
Think
m=l++||i++ && j++ && k++;
as
m = ( (l++)|| ( (i++ && j++) && (k++) ) );
As you know &&
has higher operator precedence, therefore i++ && j++ && k++
will be grouped as (i++ && j++) && k++
and then ( (i++ && j++) && (k++) )
.
Because of the short-circuit behavior of ||
, after evaluating l++
, which is true
and therefore the right operand ( (i++ && j++) && (k++) ) of ||
is not evaluated.