-5
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 || ?

Elfayer
  • 4,411
  • 9
  • 46
  • 76
Abhi
  • 73
  • 1
  • 6

2 Answers2

1

See Short-circuit evaluation.

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.

roostersign
  • 160
  • 2
  • 3
  • 16
  • Does it mean that in case of Short circuit eval priority of the operators are ignored? – Abhi Jul 09 '14 at 14:19
  • You state `l++=3`? This is post increment, thus `l++=2` I guess? – Willem Van Onsem Jul 09 '14 at 14:26
  • The question linked above actually goes into detail about [sequence points](https://en.wikipedia.org/wiki/Sequence_point). In this case, this means that the first part before || gets completely evaluated before the second part (so l would be considered 3 before the ||); it takes priority over the operator precedence. I think reading the wikipedia article and the other q&a linked would help further explain. – roostersign Jul 09 '14 at 14:50
1

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.

haccks
  • 104,019
  • 25
  • 176
  • 264