0xFF != '\xFF' ?

Wolfram Roesler wolfram at cip-s02.informatik.rwth-aachen.de
Thu Apr 18 18:47:22 AEST 1991


Sepp at ppcger.ppc.sub.org (Josef Wolf) writes:

>wolfram at cip-s08.informatik.rwth-aachen.de (Wolfram Roesler) writes:
>] It's best to compare in the following way:
>] 	char x = -1;
>] 	char y = 0xff;
>] 	if ((unsigned char)x = (unsigned char)y)
>] 	  ...
>] ... so before comparing, cast both to unsigned char. This because you
>] do not know (because it's undefined by the language definition) if char
>] is unsigned or not.

>You even don't know about the number of bits of a char (or is it defined?)
>So you can get into trouble with this.

A char is defined to contain a single character, so this is usually 8 bits.
However, I dont think my prg will cause trouble since 
	char x = -1
includes an implicit cast. Assume a char is 8 bits and an int is 16, then
this line will not simply copy the low 8 bits of -1 into x, but it will
do this preserving the sign (if chars are signed, which we assume here).
Your compiler might give a warning about signed assignement to an unsigned
var, but that's all.



More information about the Comp.lang.c mailing list