Why you get the wrong result?
I compiled:
n (int argc, char *argv[])
{
int a = 1 ;
printf ( "%d %d %d\n",a, ++a, a++ ) ;
a = 1 ;
printf ( "%d %d %d\n",a++, ++a, a ) ;
return 0;
}
And I got the right result: 331 and 221.
The output is deterministic in C, function parameters are evaluated and pushed on the stack from right to left, the printf function shows the values as specified in the format string (from left to right).
The code flow is simple as follows:
1st printf
- push the value of parameter (a) then increment it (postincrement): push 1, a=a+1=2
- Increment the variable (preincrement) then push the value: a=a+1=3, push 3
- Just push the variable: push 3
The printout is 331
2nd printf
- Just push the variable: push 1
- Increment the variable (preincrement) then push the value: a=a+1=2, push 2
- push the value of parameter (a) then increment it (postincrement): push 2, a=a+1=3
The printout is 221
printf get the values on the stack (pushed values) as input parameters.
I think that you are using a broken compiler.
If your compiler is PellesC, it is a broken outdated version: update to the last version.
Anyway the correct output is the one I showed, if you get something different there is no way: that compiler is broken and not compliant.