Search code examples
c++operator-precedencepost-incrementpre-increment

Operator Precedence.. () and ++


Salute..

I have an unusual problem. Here in this table in MSDN library we can see that precedence of () is higher than ++ (Pre-increment) . but when I run this code, it seems that precedence of ++(prefex) is higher:

int main()
{
    int a=3,b=2,x;
    x=++a + (a-b);
    cout<<"x= "<<x;

    return 0;
}

and the answer is :

x=6

This happens with prefex ++ only and works as I expect with post-increment.

Is there any reason? Regards..

int main()
{
    int a=3,b=2,x;
    x=a++ + (a-b);
    cout<<"x= "<<x;

    return 0;
}

x=4

(I use Microsoft Visual C++ 2010 express)


Solution

  • As usual, this is undefined behaviour. There is no sequence point at the +, so it is not defined at what point the ++ updates a. This is not a precedence issue.