why parenthesis is unable to change c++ operator p

2020-05-10 08:54发布

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 ()

标签: c++
5条回答
啃猪蹄的小仙女
2楼-- · 2020-05-10 09:14

The expression (x++), with or without parentheses, evaluates to the previous value of x, and has the side-effect of increasing x.

If you want to see the effect of the increase then use the obscure

cout << (x++, x) << endl;
查看更多
冷血范
3楼-- · 2020-05-10 09:17

i definitely understand ++x guys, my question is about change operator precedence using ()

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:

 

  1. 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 pass 3+5 to the multiplication operator, or do we pass 5*7 to the addition operator? It's about parsing.

     

  2. Evaluation order

    Each operand then needs to be evaluated to produce a value (e.g. 3+5 becomes 8, or 5*7 becomes 35). 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".)

     

  3. 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 for x.

    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.

查看更多
你好瞎i
4楼-- · 2020-05-10 09:19

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:

cout << ++x << endl;
查看更多
一纸荒年 Trace。
5楼-- · 2020-05-10 09:37

The value of x++ is the value of x 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.

edit: i definitely understand ++x guys, my question is about change operator precedence using ()

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 from x++. You could change order of the operators if you wrote

(cout << x)++ << endl;

but that would try to call ++ on the stream...

查看更多
够拽才男人
6楼-- · 2020-05-10 09:39

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

查看更多
登录 后发表回答