1

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

#include< stdio.h >

int main()
{
    int i = 1;
    int x = ++i * ++i * ++i;
    printf("%d\n", x);
    printf("%d\n\n",i);

    return 0;
}

Im getting output of 1!! and 4 in gcc. I use ubuntu linux

Community
  • 1
  • 1
siu
  • 312
  • 1
  • 5
  • 10
  • Well, I'm getting "warning: operation on 'i' may be undefined" when I compile with `gcc -Wall`. You should use -Wall and pay attention to the warnings. – sverre Jun 07 '11 at 09:59
  • There's a C++-faq question on this: http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – Steve Jessop Jun 07 '11 at 10:57

2 Answers2

2

Undefined Behaviour this is:

int x = ++i * ++i * ++i;

Don't do it!!!!

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
2

The behaviour of your code is undefined since i is modified more than once between sequence points:

int x = ++i * ++i * ++i;

See the FAQ (I urge you to read the entire section 3).

NPE
  • 486,780
  • 108
  • 951
  • 1,012