Operator Precedence.. () and ++
Asked Answered
B

8

1

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)

Bonnybonnyclabber answered 4/2, 2011 at 12:26 Comment(2)
Check this FAQ question for more info about sequence pointsRecorder
The table you are using is for JScript. Use the one for c++. msdn.microsoft.com/en-us/library/126fe14k%28v=vs.10.0%29.aspxCleanse
C
8

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.

Compaction answered 4/2, 2011 at 12:29 Comment(1)
@Bonnybonnyclabber - In this order, you've pasted from MSDN, there's no operator++ (: operator++ changes the value of the variable, any changes of variables should be separated with sequence point, to be everything well-defined, and you don't have a sequence point here.Excerpt
E
5

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.

Embolism answered 4/2, 2011 at 12:36 Comment(4)
The table says: "Function calls, and expression grouping" so I think they do mean both uses of parentheses. Admittedly, it is a JScript table so its relevance is questionable.Cyna
You're right, I'd overlooked that in the table. In that case, the table is wrong :-). Parentheses, when used for expression grouping, are not an operator, and it makes no sense to assign them a precedence (in JScript or anywhere else). The question whether parentheses take precedence over operators doesn't arise, as there's no ambiguity in the syntax with respect to parentheses -- whereas this question does arise for all operators, including the function call operator (), and hence these operators need to be assigned precedences. (Also, operators "do something"; parentheses are just syntax.)Embolism
Why are we discussing about JScript operator precedence when the question is about c++? Why not use the correct precedence table for that? msdn.microsoft.com/en-us/library/126fe14k%28v=vs.10.0%29.aspxCleanse
Interesting -- they don't consider grouping parentheses as operators in that table. Of course this isn't a difference between JScript and C++, it's just that the JScript table is wrong and the C++ table is right :-)Embolism
E
4

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?

Excerpt answered 4/2, 2011 at 12:29 Comment(2)
It's not just undefined which will be evaluated first; the behavior of the program as a whole is entirely undefined, under a strict reading of the standard.Sluggard
I thought the first example was good.. The Edit almost made it more complex :PRecorder
C
2

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.

Cyna answered 4/2, 2011 at 12:31 Comment(0)
S
2

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.

Slicker answered 4/2, 2011 at 12:35 Comment(0)
T
2

I will just give you a link:

Sequence Points

explained on StackOverflow.com by Prasoon Saurav

Tersina answered 4/2, 2011 at 13:39 Comment(0)
H
0

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.

Halyard answered 4/2, 2011 at 14:15 Comment(0)
E
0

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.

Erotica answered 3/2, 2016 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.