is it really necessary for character values to be positive?

Ray Butterworth rbutterworth at watmath.UUCP
Wed Dec 24 02:22:38 AEST 1986


In article <39 at houligan.UUCP>, dave at murphy.UUCP writes:
> In short, it doesn't look to me like there is any good reason to require
> characters to be represented as positive values.  Or have I overlooked
> something really basic?

Consider the following:
    char str[5];
    int i;
    i=getchar();
    str[0]=i;
    if (i!=str[0])
        printf("Is this possible?");
    if (isupper(i) && !isupper(str[0]))
        printf("How about this?");

The answer is that yes, it is possible when getchar() is allowed to
return characters that have the upper bit on, on compilers that
sign extend.  If the character is 0xF0 say, "i" will be assigned
0xF0, but str[0] will have the value 0xFFFFFFF0, and the comparison
will fail.
Similarly, many functions such as isupper() will behave incorrectly
since they aren't defined to work on negative arguments.

If ANSI were to define getchar() to return a value that is sign
extended on machines that sign-extend chars, and define functions
such as isupper() to accept such arguments, I think it would solve
most problems of sign-extension and 8-bit character sets.  It probably
wouldn't break any existing source code either (except for code that
stupidly ignores the EOF manifest and uses an explicit value).

>
> 4. The value 255 can't be used because it may look like EOF on some systems.
The only requirement on EOF is that it be a negative int.
If the implementors make it say, (-12345), it won't be confused with
any character, with or without sign extension.



More information about the Comp.lang.c mailing list