Following is the code that I run in ubuntu 13.10. Code:-
#include<stdio.h>
main()
{
int i=10,j=10;
i=i++ + ++j;
printf("i=%d j=%d\n",i,j);
j=++i + j++;
printf("i=%d j=%d\n",i,j);
}
Output:-
i=21 j=11
i=22 j=33
Logically,as per rules ans should be:-
i=22 j=11
i=23 j=35
And when I run this code in ubuntu 12.10 then i get correct ans i.e. above ans. Please explain what is happening??
++
is called postfix increment and returns the original value of the variable, not including the increment. Prepending++
is called prefix increment and will return the value of the variable after increment. Therefore, the first assignment becomes:i=10+11;
. – Nathan Osman Mar 22 '14 at 05:24i=22 j=11
andi=23 j=35
..i = (i = i + 1) + ++j;
i = (i = 10 + 1) + (j = j + 1);
i = 11 + 11;
soi = 22 j = 11
.. – rusty Mar 22 '14 at 06:23