Theory of Pure C, chapter 937 - (nf)

utzoo!decvax!cca!ima!johnl utzoo!decvax!cca!ima!johnl
Thu Dec 23 03:23:29 AEST 1982

ima!johnl    Dec 22 12:37:00 1982

Several people have written to me saying that they miss the point of this
exercise, so here it is again, clearer:

int x;  /* global definition */

foo() {         bar(x++); }

bar(arg) {      if(x == arg) ... ; }

The question is what value bar() sees for the global variable "x".  The
argument to bar() is definitely the old value of x, but the code
generated could be either of these:

	(1)     tmp = x, ++x, bar(tmp);
	(2)     bar(x), ++x;

Note that in (1), bar() sees the new value of x, while in (2) it sees the
old value.  The reason I asked was that I was poking through PCC and
there seems to be hackery to force it to do (1), even though on many
machines the code for (2) would be shorter.  The official rule appears to
be that the ++ will be done at least as soon as the next comma operator
(not comma-argument-separator) or semicolon, so it's ambiguous just like:

	foo[i++] = i;

Are there lots of sleazy programs out there that depend on this kind of
thing?  Perhaps it's too close to Christmas for this much subtlety.

If you still don't get it, reply to me, not the net.

John Levine, decvax!yale-co!jrl, ucbvax!cbosgd!ima!johnl, Levine at YALE (arpa).

More information about the Comp.lang.c mailing list