TO C OR NOT TO *C

Henry Spencer henry at utzoo.uucp
Tue Oct 17 03:22:49 AEST 1989


In article <16107 at nswitgould.cs.uts.oz> jon_hogan-doran%713.600 at fidogate.fido.oz (Jon Hogan-doran) writes:
>... it said (something like this) that getc(fd)
>returned an int value .. and yet later on he gave an example
>(something like this):
> 
>char ch;
>ch=getc(ttyd);

Barring unusual situations, his example is simply wrong.  getc does return
an int.  The value will fit in a char except in one important case:  EOF.
Unless you have reason to be *absolutely certain* that end-of-file will
not occur when attempting to read that character, the result of getc should
*never* be assigned to a char without first testing to see if it is equal
to EOF.  That usually means assigning it to an int first, so you can test
the value and then use it.
-- 
A bit of tolerance is worth a  |     Henry Spencer at U of Toronto Zoology
megabyte of flaming.           | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list