type of character constants

Guy Harris guy at auspex.UUCP
Tue Mar 21 08:36:26 AEST 1989


>>There is a subtle difference. If 'c' is an integer constant, '\377'
>>represents the value 255. If, on the other hand, it is a char
>>constant, and characters sign extend, it represents -1.

As pointed out in other articles, the statement you're quoting here from
the previous posting isn't quite correct....

>As noted above, most compilers I've used believe that '\377' *is* -1; in
>fact the only one I can recall that makes it 255 is that supplied with AIX.
>
>So what is the 'correct' value for '\377', 255 or -1?

It depends.  To quote from the December 7, 1988 dpANS:

	If an integer character constant contains a single character or
	escape sequence, its value is the one that results when an
	object of type "char" whose value is that of the single
	character or escape sequence is converted to type "int".

An example would be:

	char c = '\377';	/* "an object of type 'char' whose value
				   is that of the single character or
				   escape sequence" */

	printf("%d\n", c);	/* "is converted to type 'int'" in this
				   code, due to the usual argument
				   promotions */

If the implementation sign-extends "char"s, this should print -1 (we
assume two's complement arithmetic here; this may or may not make a
difference - I'm not about to dive that deeply into the dpANS right
now); if it does not sign-extend "char"s, this should print 255. 

The result it prints is the correct value for '\377' on that
implementation.  More than one sign-extending implementation exists;
more than one non-sign-extending exists.  UNIX implementations of both
flavors exist; many individual copies of both flavors exist.  (In other
words, AIX ain't an isolated instance, not even on e.g. the RT PC. 
Don't make your code depend on whether "char"s are signed or unsigned.)



More information about the Comp.lang.c mailing list