Omkar
BAN USERWhen I compile the code on gcc compiler and run it in a Unix environment I am getting the answer as 12,32 and 50.
One way to break down the problem is as follows:
First printf: During this stage the ++i causes i to become 6 so the addition is 6+6 = 12 and then i++ is executed causing i to be 7.
Second printf: Here again the ++i causes i to be 8 and so the addition is 8+8+8+8 = 32 and then i++ is executed three times causing i to be 11
Third printf: This is somewhat tricky part. Here the first ++i causes i to be 12 and hence i remains 12 till the second ++i which causes it to become 13 so in conclusion the addition becomes: 12 + 12 + 13 + 13 = 50 and now after this statement we have the two i++ which cause i to become 15 in the end.
This is the only explanation i can think of. Also you can verify this by inserting alternate printf statements in your existing code to print values of i at different stages.
@manishs747
- Omkar June 25, 2012If this was an interview question then what kind of interview: phone or in-person? And was this question for fresh college grads?