stack quirk?

Steve Summit scs at adam.mit.edu
Thu Aug 2 14:54:52 AEST 1990


In article <31530012 at hpcvia.CV.HP.COM> brianh at hpcvia.CV.HP.COM (brian_helterline) writes:
(in response to a code fragment like printf("%d %d\n", i++, i++);)

>	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.

Though correct for many machines, this is not true in general,
for the C language neither mandates stack direction nor the
existence of a stack.  When you hear that evaluation order, and
hence the behavior of an expression containing multiple side
effects, is "undefined," it means that you cannot predict and
should not know how various machines will do it, and that
different machines may do it differently.  To quote our good
friends K&R, "Naturally, it is necessary to know what things to
avoid, but if you don't know _how_ they are done on various
machines, that innocence may help to protect you."

>	I am not certain as to what ANSI specifies about whether this
>	behavior is correct or not.

ANSI C reaffirms that evaluation order in such cases is
undefined, and furthermore (so I have been told) gives compilers
license to reject programs depending on undefined evaluation
order at compile time, without even flipping a coin and picking
an order.

>	It is usually a bad idea to use ++ operators
>	on the same variable more than once in _ANY_ expression.

Correct, except that the operative adverb is "always."

See the frequently asked questions list for a bit more on this
topic.

                                            Steve Summit
                                            scs at adam.mit.edu



More information about the Comp.lang.c mailing list