What's a C expert?

Jeffrey Kegler jeffrey at algor2.uu.net
Fri Aug 18 06:22:10 AEST 1989


In article <757 at uniol.UUCP> henseler at uniol.UUCP (Herwig Henseler) writes:
>
>	int i = 0;
>	i += ++i + i++;
>	printf( "%d\n", i );
>
>Every number between 1 and 5 (inclusive) is legal!

Along with a bunch of others.  The ANSI C standard says the above is
undefined (3.3, with footnote and A.6.2).  This means anything may
happen.  In particular, any number or no number may be printed.

Actually, the sentence stating this is somewhat mysterious.  "Between
the previous and next sequence point an object shall have its stored
value modified at most once by the evaluation of the expression."  The
footnote and appendix make it clear that it is intended that
expression like the example make a program undefined.  But they are
not supposed to be part of the Standard, and if the sentence is read
alone, it yields other interpretations.  More obvious than its
intended meaning is that an expression like the example is legal,
yields an implementation dependent result, and, if interrupted by a
signal, must either have its initial value or that result.  In fact, I
do not think the sentence, taken alone, says what it is intended to
mean.

If we generalize to longer expressions without sequence points, such
as

i = 15 + (j = i++ + j++ + (i = j++ ... ad nauseam ) )

this imposes impressive requirements on a comforming implementation.
-- 

Jeffrey Kegler, Independent UNIX Consultant, Algorists, Inc.
jeffrey at algor2.ALGORISTS.COM or uunet!algor2!jeffrey
1762 Wainwright DR, Reston VA 22090



More information about the Comp.lang.c mailing list