why parenthesis is unable to change c++ operator precedence in this case?

前端 未结 5 672
野的像风
野的像风 2021-01-29 15:24

Here\'s my simple code:

int main()
{
    int x = 5;
    cout << (x++) << endl;

    return 0;
}

the code above prints 5

5条回答
  •  庸人自扰
    2021-01-29 16:00

    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;
    

提交回复
热议问题