is it really necessary for character values to be positive?

dave at murphy.UUCP dave at murphy.UUCP
Thu Jan 15 03:54:10 AEST 1987


Summary: does EOF have to be -1?

In article <289 at haddock.UUCP>, karl at haddock.ISC.COM.UUCP (Karl Heuer) types
(in response to an earlier article that I wrote):

>Suppose I am using such a system, and one of the characters -- call it '@' --
>has a negative value.  The following program will not work:
>    main() { int c; ... c = getchar(); ... if (c == '@') ... }
>Note that getchar() returns an UNSIGNED char on success; this is to guarantee
>that none of them compare equal to EOF.  Thus, any printing character that I
>want to enclose in single quotes had better be positive, or it becomes VERY
>awkward to use.

Thanks for pointing this out, but I don't see where it should cause a major
problem.  Assuming that the character set in use doesn't take up the entire
range of int values, all that is necessary is to pick a value for EOF that
doesn't correspond to any character value.  (Newcomers: keep in mind that
the return value of getchar and getc is defined as being an int, not a
char, even though it is often treated like a char.)  This way, getchar can
return a possibly negative value, and EOF won't collide with any legit
character value.

Would defining EOF to be something other than -1 cause a problem?  I don't
think so.  K&R says, on p. 144: "The standard library defines the symbolic
constant EOF to be -1 (with a #define in the file 'stdio.h'), but tests
should be written in terms of EOF, not -1, so as to be independent of the
specific value."  I don't this is a situation like with NULL where the
actual value has a special meaning in the language definition, so I don't
see why it couldn't be changed.  People who are testing for -1 or for a
negative value instead of using EOF deserve whatever they get.

If anyone knows of any reason why the value of EOF can't be implementation-
specific, I'd like to hear about it.
---
"I used to be able to sing the blues, but now I have too much money."
-- Bruce Dickinson

Dave Cornutt, Gould Computer Systems, Ft. Lauderdale, FL
UUCP:  ...!{sun,pur-ee,brl-bmd,bcopen}!gould!dcornutt
 or ...!{ucf-cs,allegra,codas}!novavax!houligan!dcornutt
ARPA: dcornutt at gswd-vms.arpa (I'm not sure how well this works)

"The opinions expressed herein are not necessarily those of my employer,
not necessarily mine, and probably not necessary."



More information about the Comp.lang.c mailing list