++ operator

Martin Minow minow at decvax.UUCP
Tue Dec 27 07:16:56 AEST 1983


Mark Plotnik asks for a "precise, consistent definition" of the
semantics of ++ and -- (among other things).  The definition
is "compiler dependent", but all side effect operators will
be evaluated when control passes to the next statement.
(It may be the case that side-effect operations will be evaluated
before passing over , && or ||, but I wouldn't bet on it.

Vax-11 C (on VMS) is very willing to evaluate auto-increment
operators at "unusual" times.  For example, consider the following:

	int	stack[123];		/* Evaluation stack	*/
	int	*stack_pointer = stack;
	#define push(x)	(*--stack = x)	/* Stuff into the stack	*/
	#define pop()	(*stack++)	/* Get from the stack	*/

	int
	eval(operator)
	{
		switch (operator) {
		...
		case OP_ADD:
		     push(pop() + pop());
	...

You may think that "*--stack = *stack++ + *stack++;" will "do the
right thing", but it is (by demonstration) compiler dependent.
(I found that out the hard way -- the original had the misleading
comment "don't need a temp variable since addition is commutative").
The correct expression of the above is:

		    temp = pop();
		    temp += pop();
		    push(temp);

Martin Minow
decvax!minow



More information about the Comp.lang.c mailing list