Re: increasing var twice in a statement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux