increasing var twice in a statement

jlh jlh@gmx.ch
Fri Jun 15 20:56:00 GMT 2007


Chris Wolstenholme wrote:
> 3) The operator ++ on the right hand side of the + operator is
> executed first by rules of precedence.
> 4) The operator ++ on the left hand side of the + operator is
> then executed.

It is not specified in which order the operands of the + operator
need to be evaluated.  In particular, in the expression f() + g()
you may not expect the two functions to be called in a specific
order.

Christian Böhme wrote:
> ... which would yield 9 (= 5 + 4) according to the above.

Without knowing for sure, the prefix increment merely means that
the variable is to be incremented before it's being used or read.
This means that the second increment operation _may_ happen in
between that first increment and the first reading of the variable
for the + operation (which is the case with GCC).  Anything
between two sequence points may be reordered as the compiler wants
(potentially allowing more/better optimization), as long as the
defined semantics stay the same.

As a side note, general questions about the C language that are
not related to GCC would better go to a place like comp.std.c.

jlh



More information about the Gcc-help mailing list