increasing var twice in a statement
ivan@0x4849.net
ivan@0x4849.net
Sat Jun 16 20:33:00 GMT 2007
--- Chris Wolstenholme <chris@blueband.demon.co.uk> wrote:
> My reasoning was as follows:
>
> 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.
> 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.
> 5) Then the operator + is executing adding b (now 5) to b (still 5).
>
> That's only two increments of b, but both before the addition operator.
This has been answered by others, but for emphaphis I want too reiterate that
according to the C and C++ language rules any two modifications of the same
variable between sequence points produces undefined behavior.
a = ++b + ++b;
This is undefined behavior in both C and C++. Undefined behavior in the C++
standard is defined as "behavior, such as might arise upon use of an erroneous
program construct or erroneous data, for which this International
Standard imposes no requirements."
Hope that helps,
---------------------
Ivan Novick
http://www.0x4849.net
More information about the Gcc-help
mailing list