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 itself in one statement? I compiled and executed this. x
is still 7 even after the entire statement. In my book, it says that x
is incremented!
The only difference between the two is their return value. The former increments ( ++ ) first, then returns the value of x , thus ++x .
the ++ is an unary (icremental) operator which increase the value of the variable x by 1. By the definition, x++ is a postfix form. The variables value is first used in the expression and then it is incremented after the operation. In simple, it is shorthand for i += 1.
Syntax: a = ++x; Here, if the value of 'x' is 10 then the value of 'a' will be 11 because the value of 'x' gets modified before using it in the expression.
x-- is postfix decrement operator, this means that the value of x is first used in the program and then decremented. X++ and ++x are equivalent to x = x + 1 and x-- and --x are equivalent to x = x - 1.
x = x++;
is equivalent to
int tmp = x; x++; x = tmp;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With