stack quirk?

Stephen Clamage steve at taumet.com
Sun Jul 29 08:18:46 AEST 1990


brianh at hpcvia.CV.HP.COM (brian_helterline) writes:

|>printf("\n%d %d\n", ++i, ++i);
|> etc

|	The output you got should is correct.  The arguments to
|	printf() [or any function] are pushed on to the stack
|	right to left so they are in the correct order for
|	printf() to pop them off when needed.  If you evaluate
|	each expression above right-to-left, you will "expect"
|	exactly what the output was.

There is no guarantee in what order ++i and i++ will be evaluated,
nor whether the incremented values will be stored before or after
the second of the two is evaluated.  Consequently, there is no
way to predict what the output will be.  The ANSI C standard
explicitly says that you cannot count on when the "side effects"
will take place, except that they will be complete before the
function is called.  C texts warn (or should warn) about modifying
a variable which is referenced more than once in the same expression.
Such code is often not warned-about by compilers, and may produce
surprisingly different results on different systems.

-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list