0
int x=1; x=x++; s.op.(x);
Why the answer is 1? I feel it should be 2
6 Answers
+ 15
The difference between x++ and ++x
 ⢠The prefix increment returns the value of a variable after it has been incremented.
⢠On the other hand, the more commonly used postfix increment returns the value of a variable before it has been incremented.
  prefix:  ++x;
  postfix: x++;
To remember this rule, I think about the syntax of the two. When one types in the prefix increment, one says ++x.
The position of the ++ is important here.
Saying ++x means to increment (++) first 'then' return the value of x, thus we have
++x.
The postfix increment works conversely. Saying x++ means to return the value of x first 'then' increment (++) it after, thus x++.
+ 12
Rohit Ahuja
Here is an example,đ
https://code.sololearn.com/chPAcDs7YZgV/?ref=app
// Check out the comments!đ
+ 7
Postfix
x= 1
x= (x)++ , x gets evaluated and returns 1
x= (x++) , x++ gets evaluated and x is incremented to 2, x=2
x= 1 , x is assigned to 1, overriding previous value of 2
Here you will find great explanations with nice examples by LukArToDo
https://code.sololearn.com/WFpSg9NXROI8/?ref=app
+ 1
Let's point out that since this code is confusing, it is best to just not use it. Use statements that are inherently clear instead.
But to explain why you obtain this result if you still choose to do that:
x = x++; is equivalent to this:
int temp = x++;
x = temp;
and int temp = x++; is equivalent to:
int temp = x;
x = x + 1;
So the whole x = x++; is equivalent to:
int temp = x;
x = x + 1;
x = temp;
And with this, we can see that x = x + 1; will be ignored considering that afterwards we set yet another value to x.
Fundamentally, x = x++; will never change the value of x, even if we're more or less instructing the computer to make some changes to x before putting it back to what it originally was.
+ 1
Thanx to all