When I compile this code with gcc and run
int a=1;
printf("%d",(a=a+1)+(a=a+1)+(a=a+1));
I expect the result to be 2+3+4=9, but the output is 10.
I know that there is undefined behavior in (++a)+(++a)+(++a)
, because the three ++
side effect can be run before all (++a) is evaluated.
But I think the value of a=a+1
is exactly what a
is after the assignment is evaluated. So the compiler cannot process three a=a+1
first and use the value in variable a
as the value of a=a+1
after all a=a+1
evaluated.
I want to know where is wrong according to the c standard.