1

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

i tried this simple C program in GCC

#include<stdio.h>
int main(){
int x=5;
printf("%d,%d,%d,%d,%d",++x,x++,x,x++,++x);
return 0;
}

and the output was 9,7,7,6,6 i traced it and assumed that it will print 6,6,7,7,9 but i found my assumption in reverse order, how come!

Community
  • 1
  • 1
Eslam Hamdy
  • 7,126
  • 27
  • 105
  • 165

1 Answers1

4

Because your program has undefined behaviour. There is no sequence point between the evaluations of function arguments, and it is undefined behaviour to mutate the same object more than once without intervening sequence point.

The program is simply ill-formed. It is not a valid C program.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084