Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!im4u!ut-sally!husc6!mit-eddie!ll-xn!ames!oliveb!epimass!csi!csib!jwhitnel From: jwhitnel@csib.UUCP Newsgroups: comp.lang.c Subject: Re: Comparing chars to constants Message-ID: <1261@csib.UUCP> Date: Wed, 21-Oct-87 12:56:35 EDT Article-I.D.: csib.1261 Posted: Wed Oct 21 12:56:35 1987 Date-Received: Sat, 24-Oct-87 21:21:57 EDT References: <4663@elroy.Jpl.Nasa.Gov> <7373@alice.UUCP> Reply-To: jwhitnel@csib.UUCP (Jerry Whitnell) Organization: Communications Solutions Inc., San Jose, Ca Lines: 33 In article <7373@alice.UUCP> ark@alice.UUCP writes: |In article <4663@elroy.Jpl.Nasa.Gov>, david@elroy.UUCP writes: |> From 6.6 both sides are converted into ints, 0x80 stays the |> same but if p is equal to 0x80 it may always be sign extended on |> some machines depending on its character sign convientions. | |(the original question had to do with the expression c==0x80 where | c is a character variable) | |Chars are indeed extended to ints for comparison, and that |extension may or may not involve sign extension. Saying | | c == (char) 0x80 | |may indeed be more portable, as one would hope 0x80 will be |converted to a char and then undergo the same sign exension |as c, but I wouldn't want to bet on the compiler getting it |right. Instead, consider making c an unsigned char or casting it: | | ((unsigned char) c) == 0x80 | |(the outer parentheses are there for clarity) If portability is of concern, some compilers don't support unsigned chars. The preferred technique is ( ( (int) c ) & 0xff ) == 0x80 The (int) cast can be left off. Jerry Whitnell It's a damn poor mind that can only Communication Solutions, Inc. think of one way to spell a word. -- Andrew Jackson