Here's my simple code:
int main()
{
int x = 5;
cout << (x++) << endl;
return 0;
}
the code above prints 5
, not 6, even with the parenthesis, My thought is x = x + 1 be executed first before it is printed out? can anyone explain to me what's going on here? Thank you
edit: i definitely understand ++x guys, my question is about change operator precedence using ()
The expression
(x++)
, with or without parentheses, evaluates to the previous value ofx
, and has the side-effect of increasingx
.If you want to see the effect of the increase then use the obscure
Operator precedence has nothing to do with this.
The misunderstanding probably isn't your fault: you've likely been mistaught. Your teacher(s) told you that an operand with a higher precedence, than some other operand, will be "executed first".
While this is a common explanation in schools, it is not true.
There are three things that can change the meaning of an expression in this sense:
Operator precedence
This is merely a set of rules that tell us, and tell the compiler, which operands go to which operator. Like, in
3 + 5 * 7
, do we pass3+5
to the multiplication operator, or do we pass5*7
to the addition operator? It's about parsing.Evaluation order
Each operand then needs to be evaluated to produce a value (e.g.
3+5
becomes8
, or5*7
becomes35
). The rules on the order in which these evaluations happen are quite complicated in C++, more so than you might expect, but you usually don't have to worry about them unless you're doing crazy things in between sequence points (to borrow pre-C++11 parlance).(This is the closest you'll get to a notion of "will be executed first".)
The meaning of the operator
This is where you're coming unstuck here. The meaning of the postfix increment operator
x++
is "increment x, and evaluate to the old value". Period. Full stop.It doesn't matter which operator precedence rules led to the expression
x++
being evaluated (as opposed to some other interpretation of the symbols in your code): when it's evaluated, whenever it's evaluated, you get the old value forx
.The meaning of the prefix increment operator
++x
, however, is "increment x, and evaluate to the new value", and that's the behaviour you want, so that's the code you should write.Ultimately, what sequence of computer instructions actually produces this behaviour is completely up to the compiler, and can be surprising. You shouldn't worry about it, as long as the program's result is as specified in the standard.
So just forget about this "will be executed first" stuff; it's rubbish.
That is because, it is unrelated to operator precedence. The post-increment operator
++
, as opposed to the pre-increment operator, increments its operand after its evaluation.So what you see is normal and that behavior cannot be changed by introducing enclosing parenthesis around the variable. If you want the opposite to happen, then you should use the pre-increment operator like the following:
The value of
x++
is the value ofx
before incrementing, no matter how many brackets you put. This has nothing to do with operator precendence, but this is just how post increment is defined.I already mentioned it, but to be clear: The value you see has little to do with operator precedence. With or without brackets
++
comes before<<
. Even if this wasnt the case it would not change the value you get fromx++
. You could change order of the operators if you wrotebut that would try to call
++
on the stream...This is because
x++
evaluates the value of x (5 in your case) and will increase its value after that....what you are looking for is the pre-increment, also
++x