On Sat, May 16, 2020 at 10:28 AM Paul Winalski <paul.winalski@gmail.com> wrote:
> On Fri, May 15, 2020 at 4:02 PM <ron@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