ballou@brahms (Kenneth R. Ballou) (11/02/86)
In article <4142@ism780c.UUCP> marv@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