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

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

    So this means: x++ is not equal to x = x+1

    because:

    int x = 7; x = x++;
    x is 7
    
    int x = 7; x = x = x+1;
    x is 8
    

    and now it seems a bit strange:

    int x = 7; x = x+=1;
    x is 8
    

    very compiler dependent!

提交回复
热议问题