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

后端 未结 17 1600
佛祖请我去吃肉
佛祖请我去吃肉 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:48

    x = x++;

    This is the post-increment operator. It should be understood as "Use the operand's value and then increment the operand".

    If you want the reverse to happen i.e "Increment the operand and then use the operand's value", you must use the pre-increment operator as shown below.

    x = ++x;

    This operator first increments the value of x by 1 and then assigns the value back to x.

提交回复
热议问题