This is not undefined behavior, and it is not a mistake. From the MSDN Documentation :
The increment operator (++) increments its operand by 1. The increment operator can appear before or after its operand. The first form is a prefix increment operation. The result of the operation is the value of the operand after it has been incremented. The second form is a postfix increment operation. The result of the operation is the value of the operand before it has been incremented.
Thus, MSDN reports that if you use this syntax (postfix):
a = a++;
Then the result of the operation will be assigned a a , and then increase. However, since the assignment operation has already taken place, you lose the result of the increment.
Using it (prefix):
a = ++a;
First, a will increase, then assign the incremental value of a .
EDIT
I will try to break it this way, I hope you understand better.
First of all, be aware that ++ always returns a value. If you are using a prefix version (e.g. ++a ), it returns the value a+1 . If you are using a postfix version (e.g. a++ ), it returns a before the increment occurs.
When executing this code:
int a = 10; a = a++;
You tell the compiler to assign a to a before incrementing. Therefore, a is 10 after that. An increment of 11 is lost at the bottom. Please note that you are not assigning 11 to a . You assign the old value before the increment, so you get pin 10 .
When executing this code:
int a = 10; int b = a++;
After completing the last line, it increments a equal to 11 and assigning 10 'b'. Since you are assigning another variable, a will not be overwritten with the original value of 10 , as in the first example.
To make it even more visual, see here:
a = a++; ^ a is increased to 11, but the postfix increment returns the old value (10)
This line effectively becomes:
a = 10; int b = a++; ^ a is increased to 11, but b gets assigned a old value
This line effectively becomes:
int b = 10; a = a + 1;
Does this make it clear?