0xFF != '\xFF' ?

Dale Worley worley at compass.com
Thu May 9 00:03:02 AEST 1991


In article <wolfram.673701563 at cip-s01> wolfram at cip-s01.informatik.rwth-aachen.de (Wolfram Roesler) writes:
   Subject: Re: 0xFF != '\xFF' ?

   BTW, isnt a char defined to contain at least 8 bits in Ansi-C?
   I read something about that in a (however rather unprecise) book.

Yes, chars are required to have at least 8 bits.  See section 2.2.4.2
of the standard.

However beware that "character constants" in C really have type int.
The standard states: "If [a] character constant contains a single
character or escape sequence, its value is the one that results when
an object with type char whose value is that of the single character
or escape sequence is converted to type int." (3.1.3.4, page 30, line
33, Dec 88 draft)  This means that if your chars are *signed*,
character constants with the high bit on will be *sign-extended* into
ints.  If you want to avoid this, you will probably have to say

	(unsigned char)'\xFF'

Then the conversion to int is done by zero-extending rather than
sign-extending.

In general, signed char's are losing, especially if you have
characters with values >127.

Dale

Dale Worley		Compass, Inc.			worley at compass.com
--
You have joined the Legion to die.  We will send you where you can die.
-- Plaque reputedly posted in mess halls of the French Foreign Legion.



More information about the Comp.lang.c mailing list