Is the behavior of return x++; defined?
It is defined.
It returns the value of x
before incrementation. If x
is a local(non-static) variable this post incrementation has no effect since local variables of a function cease to exist once the function returns. But if x
is a local static variable, global variable or an instance variable( as in your case), its value will be incremented after return.
Yes.
In postincrement
(x++) the value of x is evaluated (returned in your case) before 1 is added.
In preincrement
(++x) the value of x is evaluated after 1 is added.
Edit: You can compare the definition of pre and post increment in the links.
Yes, it's equivalent to:
int bar()
{
int temp = x;
++x;
return temp;
}
Yes it is ... it will return the x's value before incrementing it and after that the value of x will be + 1 ... if it matters.