This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Re: increasing var twice in a statement
- From: "Timothy C Prince" <tprince at myrealbox dot com>
- To: monodhs at gmx dot de
- Cc: gcc-help at gcc dot gnu dot org, chris at blueband dot demon dot co dot uk
- Date: Fri, 15 Jun 2007 20:55:42 +0000
- Subject: Re: Re: increasing var twice in a statement
- Reply-to: tprince at computer dot org
-----Original Message-----
From: Christian BÃhme <monodhs@gmx.de>
To: gcc-help@gcc.gnu.org
Date: Fri, 15 Jun 2007 22:09:10 +0200
Subject: 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.
______________________________--
The question was about what result a C standard would dictate, but the C standard in effect says no result can be relied upon in this kind of situation.
Tim Prince