Originally posted by ravish kumar:
will plz anyone will tell me wjtz goimng on here ???
int k = 1;
int i = ++k + k++ + + k ;
TIA
Ok I could be wrong here but you have to know that prefix and postfix operations are done before math operations and also the associativity that prefix unary operations are done right to left and then both postfix unary operations and normal math operations are done left to right.
So you end up with it first looking like this:
int i = (++k) + (k++) + (+k)
so then the first ++k is evaluted which is set to 2. Now since k is 2 the left associativity takes place setting k++ which means (k++) will evaluate to 2 but remember afterwards k is now 3 so in (+k) it's just doing (+3). You end up then with:
2 + 2 + 3 = 7.
Someone correct me if I'm wrong.