What is x after "x = x++"?

Michael picture Michael · Oct 27, 2011 · Viewed 40.8k times · Source

What happens (behind the curtains) when this is executed?

int x = 7;
x = x++;

That is, when a variable is post incremented and assigned to itself in one statement? I compiled and executed this. x is still 7 even after the entire statement. In my book, it says that x is incremented!

Answer

Prince John Wesley picture Prince John Wesley · Oct 27, 2011
x = x++;

is equivalent to

int tmp = x;
x++;
x = tmp;