i=2;
i= ++i + ++i + ++i;
printf(i)
Please give the output with explanation?
The answer I'm getting is 12 but it should be 13.
i=2;
i= ++i + ++i + ++i;
printf(i)
Please give the output with explanation?
The answer I'm getting is 12 but it should be 13.
The behavior of your code is undefined according to the C standard, as you are not allowed to use the preincrement operator more than once within the same expression. The output can be anything whatsoever.
See the answer to this question for a more comprehensive treatment of the topic.
Though the behaviour is undefined, IN UR CASE IT HAS BEEN EXECUTED AS, CONSIDERING THE PARSING IS FROM left, i = 5 + 4 + 3 = 12
For explanation, i = (++i) + (++i) + (++i) Now i = 2, so first ++i expands as 3 and i becomes i=3 i = (++i) + (++i) + 3
Now i = 3, so ++i expands as 4 and i becomes i=4 i = (++i) + 4 + 3
Now i = 4, so first ++i expands as 4 and i becomes i=5 i = 5 + 4 + 3