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

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

    What happens when int x = 7; x = x++;?

    ans -> x++ means first use value of x for expression and then increase it by 1.
    This is what happens in your case. The value of x on RHS is copied to variable x on LHS and then value of x is increased by 1.

    Similarly ++x means -> increase the value of x first by one and then use in expression .
    So in your case if you do x = ++x ; // where x = 7
    you will get value of 8.

    For more clarity try to find out how many printf statement will execute the following code

    while(i++ <5)   
      printf("%d" , ++i);   // This might clear your concept upto  great extend
    

提交回复
热议问题