Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mavin question -- Char data

2 views
Skip to first unread message

Kenneth R. Ballou

unread,
Nov 1, 1986, 6:51:07 PM11/1/86
to
In article <41...@ism780c.UUCP> ma...@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

0 new messages