0

Given the following code:

 int  a=0,b=1;
 int r=a+++b;

which are the operations performed and in which order?

   a++ + b
   a + ++b

I this compiler specific or does it depend on the standard?

starsplusplus
  • 1,232
  • 3
  • 19
  • 32
  • 2
    Avoid statements like this, if at all possible – Elias Van Ootegem Feb 07 '14 at 10:12
  • 2
    It is always possible to avoid pathological cases like this. When it is not obvious what it means to a human you should use other constructs to make the meaning clear (like parentheses and space). Just because the compiler can interpret this does not mean we want humans to ever try and work it out. – Martin York Feb 07 '14 at 10:24
  • @starsplusplus sorry about that; you are right. –  Mar 18 '14 at 09:00

2 Answers2

4

It is (a++) + b but not because of operator precedence.

It is parsed as (a++) + b because the compiler takes the longest token it can from a sequence of characters. In other words, the lexer keeps reading characters until it encounters something that can't be part of the same token as what it already has.

This is also how it interprets >= as one token instead of > and =, and double as 'double' not 'do uble'.

There are rules of operator precedence for statements like

a || b && c
// "a || (b && c)" NOT "(a || b) && c", because && takes precedence

However, in your case the operators ++ and + have already been determined. Once the operators have been determined, the rules of operator precedence can apply.

There are some good answers to Why doesn't a+++++b work in C? that should explain this in more detail.

Community
  • 1
  • 1
starsplusplus
  • 1,232
  • 3
  • 19
  • 32
  • I would rephrase the second sentence. `It is parsed as a ++ + b because ...`. There are no `(` or `)` in the original so you don't want to add them at this point. You can (and should) use the when explaining how the expression is evaluated (the first sentence). Note: the compiler does not always take the longest token (remember the whole issue around `>>` with templates). – Martin York Feb 07 '14 at 17:13
  • I really just meant "it is this way because", which is why I kept the syntax of the "this" the same as the previous sentence. I feel like *It is X. It is Y because...* is more confusing than *It is X. It is X because...* As for the `>>` in templates the whole reason why that is a problem is because of taking the longest token: the lexer gets `>>` when it should be two `>`s. – starsplusplus Feb 10 '14 at 12:01
1

That is governed by the operator precedence. Since postfix ++ has a higher precedence than the + operator it binds tighter to a is post incremented then added to b so the result is:

(a++) + b
DrYap
  • 6,525
  • 2
  • 31
  • 54
  • 5
    **not true**. execution *order* is governed by operator precedence. but at that point, you already have the operators. see http://stackoverflow.com/questions/7485088/what-does-the-operation-c-ab-mean – Karoly Horvath Feb 07 '14 at 10:04
  • 2
    @user3283017 As has been pointed out this answer is wrong. Please could you unmark it as accepted and accept the correct answer. – DrYap Feb 07 '14 at 14:30