What is x after “x = x++”?

后端 未结 17 1679
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 06:26

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

17条回答
  •  清歌不尽
    2020-11-21 06:54

    x does get incremented. But you are assigning the old value of x back into itself.


    x = x++;
    
    1. x++ increments x and returns its old value.
    2. x = assigns the old value back to itself.

    So in the end, x gets assigned back to its initial value.

提交回复
热议问题