Originally posted by sonir shah:
Options : 1)Compilation error at: "++k+k++ + + k" expression]
2)Compile and will print 7 and 3
3)Compile and will print 5 and 2
4)Compile and will print 9 and 3
5)Compile and will print 5 and 3
The answer is :2) It will compile and will print the value 7 and 3 when run.
I feel the asnwer is 3).
because value assigned to i will be 5 in the end and the value assigned to k will 2(twice ++k)
please explain.
Sonir
Be careful with this post/pre incrementors
people love asking questions on this . donno why
int i = ++k + k++ + + k ;
++k, increments itself and then gives it to i
so its equal to 2
k++ , gives its value 2 to the 'i' to participate in the addition ( ++k + k++) and then increments itself..
+ +k - this incremented k is again added , so the value of i is 7 and k is 3
hope you got it
Ragu