> the unary operator (++) has precedence over the arithmetic operator (*), so each ++i is evaluated first
Wrong - this is not a precedence problem at all. If it were just down to precedence, then wrapping up the increment inside a function call would make no difference to the results.
This is simply not the case.
Code:
#include <stdio.h>
/* does nothing except return the passed parameter */
/* In other words, a rather expensive No Operation */
int f ( int a ) {
return a;
}
int main ( ) {
int i,j;
i=5;
j=++i * ++i;
printf("%d\n",j);
i=5;
j= f(++i) * f(++i);
printf("%d\n",j);
i=5;
j=i++ * i++;
printf("%d\n",j);
i=5;
j= f(i++) * f(i++);
printf("%d\n",j);
return 0;
}
By your argument, if it were just down to precedence, then these would be the same - but they're not.
[tt]
VC7.NET Debug - 49,49,25,30
VC7.NET Release - 49,49,25,25
VC6 Debug - 49,42,25,30
VC6 Release - 49,49,25,30
gcc 3.3.1 Debug - 49,42,25,30
gcc 3.3.1 Release- 49,42,25,30
[/tt]
Any code which produces
- different answers between debug and release
- different answers between compilers
is wrong.
You can't debug such code (the release will be different), and you can't port your code to another compiler. Even updating your compiler will break your code.
--