Re: increasing var twice in a statement

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

 



--- Chris Wolstenholme <chris@xxxxxxxxxxxxxxxxxxxx> 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

[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