N
nl
Hi, I am confused about the order of operation for the increment
operater ++. I wrote the program below trying to clear my doubts, but
it just got me more confused. For example, in the second case, I
thought that ++x will increment x to 2. and since y = 3, that makes z
= 2 + 3 =5. However the result shows that z =4.
Would greatly appreciate any help.
#include <stdio.h>
int main(void)
{
int x=1, y=1, z=0;
z = x++ +( y += x++) ;
printf("\n\n%d %d %d\n\n", x, y, z);
x=1, y=1, z=0;
z = x++ +( y += ++x) ;
printf("\n\n%d %d %d\n\n", x, y, z);
x=1, y=1, z=0;
z = ++x +( y += x++) ;
printf("\n\n%d %d %d\n\n", x, y, z);
x=1, y=1, z=0;
z = ++x +( y += ++x) ;
printf("\n\n%d %d %d\n\n", x, y, z);
return 0;
}
The results:
2 2 3
2 3 4
3 3 5
3 4 6
operater ++. I wrote the program below trying to clear my doubts, but
it just got me more confused. For example, in the second case, I
thought that ++x will increment x to 2. and since y = 3, that makes z
= 2 + 3 =5. However the result shows that z =4.
Would greatly appreciate any help.
#include <stdio.h>
int main(void)
{
int x=1, y=1, z=0;
z = x++ +( y += x++) ;
printf("\n\n%d %d %d\n\n", x, y, z);
x=1, y=1, z=0;
z = x++ +( y += ++x) ;
printf("\n\n%d %d %d\n\n", x, y, z);
x=1, y=1, z=0;
z = ++x +( y += x++) ;
printf("\n\n%d %d %d\n\n", x, y, z);
x=1, y=1, z=0;
z = ++x +( y += ++x) ;
printf("\n\n%d %d %d\n\n", x, y, z);
return 0;
}
The results:
2 2 3
2 3 4
3 3 5
3 4 6