Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!caen!ox.com!math.fu-berlin.de!unidui!unido!rwthinf!cip-s02!wolfram From: wolfram@cip-s02.informatik.rwth-aachen.de (Wolfram Roesler) Newsgroups: comp.lang.c Subject: Re: 0xFF != '\xFF' ? Message-ID: Date: 18 Apr 91 08:47:22 GMT References: <28007837.35A9@marob.uucp> Sender: news@rwthinf.UUCP Lines: 23 Sepp@ppcger.ppc.sub.org (Josef Wolf) writes: >wolfram@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.