getc() != EOF

ShanklandJA opus at drutx.UUCP
Thu May 31 03:03:26 AEST 1984


(sigh.)

> > >	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."
> > 
> 
> 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

But it is not defined whether char is a signed of unsigned type in C.
On machines where char is unsigned, c will never have the value -1,
and the comparison with EOF will always fail.

All this is quite clearly described on page 40 of K&R.

Jim Shankland
..!ihnp4!druxy!opus



More information about the Comp.unix.wizards mailing list