0

Possible Duplicate:
Undefined Behavior and Sequence Points

I wrote the following code and [to my surprise] it printed: xx: 1 zz: 0

I would have expected both xx and zz to print as 0. This is tried with gcc 4.2, 4.4 and 4.7 on linux and darwin. Upon some research, I find that x=x++ is undefined behavior but I would expect (x++) to be executed first and return 0. Can someone shed light into this strange compiler behavior?

#include <iostream>
using namespace std;

int frwd(int aa) { return aa; }

int main() {
  int xx = 0;
  xx = (xx++);
  cout << "xx:" << xx << endl;

  int zz = 0;
  zz = frwd(zz++);
  cout << "zz:" << zz << endl;
}
Community
  • 1
  • 1
Dr.Altan
  • 151
  • 1
  • 6
  • Complete explanation at: [Undefined Behavior and Sequence Points](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) – Carl Norum Jul 27 '12 at 06:18

1 Answers1

1

Undefined behaviour is undefined. Anything could happen. Your question is a duplicate many times over.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469