getc() != EOF

Chris Maltby chris at basser.SUN
Thu May 31 02:28:01 AEST 1984


[]
> >
> >In all conscience,
> >
> >	while ( (c = getc()) != EOF )
> >
> >ought to work.  If somebody is to be blamed, it is surely not the
> >people who wrote the code, but the people who made a C implementation
> >that broke it.
> 
> It will work if "c" is declared "int."
> It will not work if "c" is declared "char."
> 
> Variable declarations are an essential part of the program, and should be
> included in illustrative code fragments, so problems are not concealed.
> 
>                                  Ed Nather

WRONG! The code above will work if c is int or char.
Char variables are promoted to int in expressions (see C manual)
and a char -1 is IDENTICAL with an int -1. Unsigned char c could be
different (Any C implementors there? (kvm?)). 

Chris Maltby
University of Sydney



More information about the Comp.unix.wizards mailing list