Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is x after "x = x++"?

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!

like image 979
Michael Avatar asked Oct 27 '11 04:10

Michael


People also ask

Is X ++ the same as ++ x?

The only difference between the two is their return value. The former increments ( ++ ) first, then returns the value of x , thus ++x .

What does X ++ do in Java?

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.

What does X ++ do in C?

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.

What is the same meaning as X ++ in C++?

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.


1 Answers

x = x++; 

is equivalent to

int tmp = x; x++; x = tmp; 
like image 149
Prince John Wesley Avatar answered Oct 09 '22 03:10

Prince John Wesley