This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: increasing var twice in a statement


Chris Wolstenholme wrote:

1) The statement was a = ++b + ++b;
2) b=3 before this statement
3) The operator ++ on the right hand side of the + operator is executed first by rules of precedence. As it is pre-increment, b becomes 4.

... yielding a (hypothetical) temporary holding the value 4 as the right hand operand of "+".

4)The operator ++ on the left hand side of the + operator is then executed. As it is also pre-increment, b is again altered to become 5.

... yielding a(nother hypothetical) temporary holding the value 5 as the left hand operand of "+".

5) Then the operator + is executing adding b (now 5) to b (still 5).

... which would yield 9 (= 5 + 4) according to the above.


That's only two increments of b, but both before the addition operator.

Try to derive a syntax tree from the expression and evaluate it by walking that tree using your defined order and you'll arrive at a = 9.


Cheers, Christian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]