Possible Duplicate:
Undefined Behavior and Sequence Points
Working out the below code by hand:
#include <stdio.h>
int func (int a, int b) {
static int c = 1;
return a + b * (c *= -1);
}
int main () {
int a = 2, b = 3;
int c = func(a, b);
a *= a++;
b *= ++b;
printf("%d %d %d %d\n", a, b, c, func(a, b));
}
I calculate the variables in printf()
to be as follows:
a = 5, b = 16, c = -1, func(a, b) = -11
however my compiler tells me the last value is in fact 21.
Output:
a = 5, b = 16, c = -1, func(a, b) = 21n
I'd calculate my value as (16*-1) + 5
Can anyone tell me where I have gone wrong?