Path: utzoo!mnetor!uunet!husc6!cmcl2!nrl-cmf!ames!oliveb!sun!limes From: limes@sun.uucp (Greg Limes) Newsgroups: comp.lang.c Subject: Re: optimization (was Re: volatile) Message-ID: <51429@sun.uucp> Date: 30 Apr 88 03:43:15 GMT References: <12578@brl-adm.ARPA> <1988Mar25.172355.348@utzoo.uucp> <20065@think.UUCP> <500@wsccs.UUCP> Reply-To: limes@sun.UUCP (Greg Limes) Organization: Sun Microsystems, Mountain View Lines: 36 In article <500@wsccs.UUCP> terry@wsccs.UUCP (Every system needs one) writes: >I would not expect > > #define EOF (-1) > unsigned char getachar(); > main() > { > while( getachar() != EOF); > ... > ... > ... > } > >to not be optimized to the equivalent of > > unsigned char getachar(); > main() > { > for( ;;) > getachar(); > } > >In fact, a good optimizer would do just that, as an unsigned char can never >be negative, by definition. In fact, I would expect the first to be optimised exactly into the second, for the precise reason you mention. When designing an input function, you MUST take boundry conditions into account; in this case, since the input stream consists of eight bit characters, the value EOF must be (by definition) out-of-band data, and therefore will not be representable in an eight bit unsigned value. Change your declaration for getachar(); it will not work even for many compilers that do absolutely no optimisation at all. It returns something larger than an unsigne char, probably an "int". -- Greg Limes [limes@sun.com] frames to /dev/fb