[TUHS] v7 K&R C

Warner Losh imp at bsdimp.com
Sun May 17 03:39:54 AEST 2020


On Sat, May 16, 2020 at 10:28 AM Paul Winalski <paul.winalski at gmail.com>
wrote:

> > On Fri, May 15, 2020 at 4:02 PM <ron at ronnatalie.com> wrote:
> >
> >Unfortunately, if c is char on a machine with unsigned chars, or it’s of
> >type unsigned char, the EOF will never be detected.
> >
> >    - while ((c = getchar()) != EOF) if (c == '\n') { /* entire record is
> now there */
>
> The function prototype for getchar() is:    int getchar(void);
>
> It returns an int, not a char.  In all likelihood this is specifically
> *because* EOF is defined as -1.  The above code works fine if c is an
> int.  One always has to be very careful when doing a typecast of a
> function return value.
>

In the early days of my involvement with FreeBSD, I went through and fixed
about a dozen cases where getopt was being assigned to a char and then
compared with EOF. I'm certain that this is why. Also EOF has to be a value
that's not representable by a character, or your 0xff bytes would disappear.

Warner
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://minnie.tuhs.org/pipermail/tuhs/attachments/20200516/4620d5c9/attachment.htm>


More information about the TUHS mailing list