is it really necessary for character values to be positive?

mouse at mcgill-vision.UUCP mouse at mcgill-vision.UUCP
Mon Jan 12 15:54:14 AEST 1987


In article <289 at haddock.UUCP>, karl at haddock.UUCP (Karl Heuer) writes:
> 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.

Well.  Now, exactly what does it mean to say that @ is negative?
Presumably it means that the test below will succeed:

	char c; /* note: not int */
	....
	c = '@';
	if (c < 0)

Now, remember that everybody (K&R and H&S and I hope ANSI) agrees that
'x' is an int, not a char.  Notice that you can't make '@' the same
thing as what getchar() returns, because the following will fail:

	char string[something];
	....
	if (string[subscript] == '@')

About the neatest solution I see is to make 'x' have type unsigned char
rather than int, at least when there's only one character between
quotes (is there any code out there *using* multi-char character
constants?).  Then we also have to arrange that char and unsigned char
are not promoted to int in expressions not involving anything bigger
than char.  This should make both of these work.

Is there anything wrong with changing the type of 'x' literals (and
fixing char-only expressions), that is, will it break anything?

					der Mouse

USA: {ihnp4,decvax,akgua,utzoo,etc}!utcsri!mcgill-vision!mouse
     think!mosart!mcgill-vision!mouse
Europe: mcvax!decvax!utcsri!mcgill-vision!mouse
ARPAnet: think!mosart!mcgill-vision!mouse at harvard.harvard.edu



More information about the Comp.lang.c mailing list