15,921,793 members
See more:
consider i=4
and I want to print the result for: ++i + ++i + i++
what would be the result and how?
Posted

## Solution 1

Well, dude, this is your homework. The answer is 17, if you have a compiler around, you can simply calculate it, just with copy-paste. But I won't tell you why. I suggest you consult the precedence table[^], and imagine how the left to right expression tree would look like... and you will have it.

Maciej Los 12-Dec-14 18:10pm
+5

## Solution 2

The problem is that the sequence of evaluating function arguments is undefined. I.e. one compiler might do it this way, another the other way. It might even change with the argument type or the number of argument.

To illustrate this: in C++ the operator can be written as function calls, e.g.:
C++
`int result = operator+(operator+(operator++(i), operator++(i)), operator++(i, 0));`
Note that `operator++(i)` is the pre-increment operator, while `operator++(i, 0)` is the post-increment operator.

Try the two cases:
A) your compiler evaluates the arguments from left to right
B) your compiler evaluates the arguments from right to left

A) see the sequence from left to right (top-down)
C++
```result = operator+            // 1. call
( operator+          // 2. eval the 1st expression...
( operator++(i)  // 3. eval the 1st expression: res = 5,         i = 5
, operator++(i)  // 4. eval the 2nd expression: res = 6,         i = 6
)                // 5.                          res = 5 + 6 = 11
, operator++(i, 0)  // 6. eval the 2nd expression: res = 6,         i = 7
)                   // 7.                          res = 11 + 6 = 17```

B) see the sequence numbers are shuffled so that the 2nd is evaluated first
C++
```result = operator+            // 1. call
( operator+          // 3. eval the 1st expression...
( operator++(i)  // 5. eval the 1st expression: res = 5,         i = 5
, operator++(i)  // 4. eval the 2nd expression: res = 4,         i = 4
)                // 6.                          res = 4 + 5 = 9
, operator++(i, 0)  // 2. eval the 2nd expression: res = 4,         i = 3
)                   // 7.                          res = 9 + 4 = 13```

The prudent conclusion is: don't do this kind of expression.
If any used variable in an expression has a side effect, it should be the only instance of that variable in the whole expression, otherwise you might experience undesired effects.

Cheers
Andi

PS: If you are interested in the standard, you might for example see open-std.org: Working Draft, Standard for Programming Language C++[^], section 1.9: Program execution, paragraph 13 and following (especially paragraph 15 and associated examples).
"[...]Value computations and side effects associated with different argument expressions are unsequenced[...]" - where "unsequenced" roughly means that they can be evaluated in any order.

v2
Maciej Los 12-Dec-14 18:11pm
All what i'm able to say is: WOW!
+5!
Andreas Gieriet 12-Dec-14 18:55pm
BTW: I was one of the crazy guys completing CPPGM (I), so I'm supposed to know the standard documents to certain degree ;-)
Cheers
Andi
Maciej Los 13-Dec-14 4:11am
Now, i need to say it twice: WOW!
CPPGM - congrats!
Andreas Gieriet 13-Dec-14 7:12am
Thanks :-)
Andi