Operator Precedence.. () and ++

白昼怎懂夜的黑 提交于 2019-12-23 04:06:23

问题


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)


回答1:


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.




回答2:


There are two misunderstandings here.

The first: In the table, () refers to function calls, not to parentheses used for grouping. Parentheses used for grouping are not an operator with a certain precedence, but a means for enforcing a different interpretation than that given by operator precedence. Thus, whatever is grouped in parentheses is treated as a unit, no matter what the precedences of the operators are.

The second: Operator precedence refers to the order of precedence the operators take in parsing otherwise ambiguous syntax; it doesn't refer to the temporal order of side effects. Thus, prefix ++ always increases the value before the expression is evaluated, posfix ++ always increases the value after the expression is evaluated, independent of syntactic operator precedences.




回答3:


This x=a++ + (a-b); is undefined behavior, no matter if it is ++a or a++.

The reason is: you're calling operator+ with two arguments: a++ and (a-b), but it's undefined which of the two arguments will be evaluated first.


EDIT: - as I see you don't understand the problem here, I'll add some more info:

operator++ (postfix or prefix, whatever), changes the value of the variable, so this makes this operator more special, that operator+, operator-, etc. So, as ++a and a++ changes the value of a, you need to have a sequence point after this, before using a again. operator+ is NOT a sequence point, so it's like you've called operator+( ++a, (a-b) );. The standard says NOTHING about the order of evaluation of the parameters, so this brings us the undefined behavior.

Better?




回答4:


Precedence does not determine order of evaluation. Precedence specifies how operators bind to operands; not the order in which the operators are evaluated.

Precedence tables are derived from the grammar, they are not a replacement for it.

Also, you shouldn't assume that a JScript precedence table necessarily has any bearing on C++ code.




回答5:


This is invoking undefined behavior. There is no sequence point between the modification of a via pre-increment or post-increment and its use in the parenthetical expression. This actually has nothing to do with operator precedence. Consider a simpler example:

int a = 1;
int b = (a=2) + a;

What should the value of b be? The compiler is allowed to evaluate the left hand and right hand sides of + in any order, because in general you'll get the same answer for both orders, except when you modify one of the variables in the expression and refer to it again with no intervening sequence point. This is, as I said, undefined behavior, one compiler might give 4, another 3, a third might light your computer on fire.




回答6:


I will just give you a link:

Sequence Points

explained on StackOverflow.com by Prasoon Saurav




回答7:


It is my understanding that when-ever you use something like a++ in a formular, a copy of value of "a" is used for the math, since "++" only works after other operations, the formula is never updated. but I may be wrong about this.

so if you have x=++a + b, where you a = 1 and b = 2 you get something like x = 1 + 2, and with x = a++ + b, you get x = 2 + 2, on the values substitution.




回答8:


It does not have any precedence issue.

If you will solve the question then whenever we have = operator it will be solved from "right to left". Means right side expression will be solved and ans will be assigned to variable x. While solving right side expression we have + operator which uses "left to right" approach for solution. So ++a will be solved first and then according to rule bracket will be solved. So ++a will generate 4 and (a-b) will give 2 so final addition will give result 6.



来源:https://stackoverflow.com/questions/4897934/operator-precedence-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!