Path: utzoo!attcan!uunet!algor2!jeffrey From: jeffrey@algor2.uu.net (Jeffrey Kegler) Newsgroups: comp.lang.c Subject: Re: What's a C expert? Summary: Section 3.3 yields further perils Message-ID: <1989Aug17.202210.5188@algor2.uu.net> Date: 17 Aug 89 20:22:10 GMT References: <12214@well.UUCP> <6057@microsoft.UUCP> <1336@atanasoff.cs.iastate.edu> <757@uniol.UUCP> Reply-To: jeffrey@algor2.UUCP (Jeffrey Kegler) Distribution: all Organization: Algorists, Inc. Lines: 36 In article <757@uniol.UUCP> henseler@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@algor2.ALGORISTS.COM or uunet!algor2!jeffrey 1762 Wainwright DR, Reston VA 22090