-2

I have been running this code at different compilers. At Microsft VS. it prints 1, but at gcc, it prints 0. What is the result according to the standard c++. I don't if there is standardization for this piece of code as well.

int a=0;
a=a++;
cout << a<< endl;
seleucia
  • 1,046
  • 4
  • 17
  • 30

1 Answers1

2

a=a++ is undefined behavior. Not only is there no standard definition for what will happen, it isn't even guaranteed to always do the same thing between different runs.

It could print 0 now, 1 the next time, and crash your program on the third attempt.

Zinki
  • 446
  • 4
  • 10