开发者

Operator Precedence.. () and ++

开发者 https://www.devze.com 2023-02-08 18:55 出处:网络
Salute.. I have an unusual problem. Here in this table in MSDN library we can see that precedence of () is higher than ++ (Pre-increment) .

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)


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.


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.


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?


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.


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.


I will just give you a link:

Sequence Points

explained on StackOverflow.com by Prasoon Saurav


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消