optimization (was Re: volatile)

Greg Limes limes at sun.uucp
Sat Apr 30 13:43:15 AEST 1988


In article <500 at wsccs.UUCP> terry at 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 at sun.com]				frames to /dev/fb



More information about the Comp.lang.c mailing list