424,967 Members | 1,710 Online
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

# Clarify the associativity

 P: 48 Expand|Select|Wrap|Line Numbers int i=-3,j=2,k=0,m;   m=++i||++j&&++k;   printf("\ni=%d""\nj=%d""\nk=%d""\nm=%d",i,j,k,m);   FOR THE ABOVE CODE my compiler gives the answer as i=-2, j=2, k= 0, m=1 At first ++i||++j is evaluated. For the || operator if one operand is non-zero the result is non-zero.hence here i is increased to -2 and ++j is not evaluated... But after this -2 should be ANDed with k. Since for the && operation both the operands should be evaluated to find the result according to me K should be increased to 1. But here the K is not increased. Kindly clarify the reason pls Aug 27 '10 #1

Parentheses make no difference as && has higher precedence than || , so they evaluate left to right, evaluating the left argument of || first, then (is if was 0) its right argument, which is ++j && ++k , not ++j alone.

4 Replies

 100+ P: 687 No. With parentheses, the expression is m=++i||(++j&&++k); First, ++i is evaluated. As it is nonzero, the result of || operation is true (1) and the rest is not evaluated at all (short-circuit evaluation). i gets incremented to -2 and the other are not changed. Aug 27 '10 #2

 P: 48 Thanks for your reply. But here the expression will be evaluated from left to right(imagine the condition without paranthesis) and hence I doubt the AND operation should take place.Kindly clarify Aug 27 '10 #3

 100+ P: 687 Parentheses make no difference as && has higher precedence than || , so they evaluate left to right, evaluating the left argument of || first, then (is if was 0) its right argument, which is ++j && ++k , not ++j alone. Aug 27 '10 #4

 P: 48 Thanks newb16.......i have understood it clearly.... Aug 29 '10 #5