Mavin question -- Char data
    Kenneth R. Ballou 
    ballou at brahms
       
    Sun Nov  2 09:51:07 AEST 1986
    
    
  
In article <4142 at ism780c.UUCP> marv at ism780c.UUCP (Marv Rubinstein) writes:
>   unsigned char uc;
>	    char c;
>
>     uc > -1  /* evaluates to 0  (for all uc) because -1 is converted to
>		 unsigned */
>
>      c > -1  /* evaluates to 1 (for all c) because c is converted to
>		 an *integer* whose range is 0..255 and the comparison is
>		 a signed compare */
>
>Now the question, is this C?
Yes, it is correct.  The results are a consequence of the rule that after
automatic widening of types has occurred, if either operand is unsigned,
then the expression is evaluated with unsigned operands.  When I was first
learning C, I made the very silly mistake of writing
    char ch;
    while ((ch = getchar) != EOF)
which would work on some compilers and not on others.  Naturally, the diffi-
culty was that in some cases 'char' by default was unsigned, and so the
comparison was done using unsigned arithmetic.
---
Kenneth R. Ballou		...!ucbvax!cartan!brahms!ballou
Dept. of Mathematics
University of California
Berkeley, California  94720
    
    
More information about the Comp.lang.c
mailing list