getc() != EOF
guy at rlgvax.UUCP
guy at rlgvax.UUCP
Sat May 26 11:39:06 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.
Assuming you're referring to the case where "c" was declared as "char" and
it didn't work, the code was incorrect. "getc" is documented as returning
an "int". The reason is that it is desirable that it can return all possible
values that fit into a "char" (in the manual page it says "Getc returns the
next character (i.e., byte)), but if it returned a "char" there would be
no distinguished value which would indicate EOF. It "ought to work" only
if 1) it is defined not to work except on 7-bit ASCII text files (or, at least,
files not containing the character '\0', or '\377', or '\351', or whatever
your choice for EOF is) or 2) it is defined as returning an "int", so that
in addition to all possible one-byte values it can also return a distinguished
value for EOF. Consider this as possibly a weak vote for languages in
which a procedure (or expression; "getc" is a macro in UNIX) can return a
success/failure indication as well as a value.
Guy Harris
{seismo,ihnp4,allegra}!rlgvax!guy
More information about the Comp.unix.wizards
mailing list