Path: utzoo!attcan!uunet!auspex!guy From: guy@auspex.UUCP (Guy Harris) Newsgroups: comp.lang.c Subject: Re: type of character constants Keywords: character constants Message-ID: <1186@auspex.UUCP> Date: 20 Mar 89 22:36:26 GMT References: <13068@steinmetz.ge.com> <102@servio.UUCP> <10138@socslgw.csl.sony.JUNET> <1783@dlvax2.datlog.co.uk> <3711@xyzzy.UUCP> <1644@vicorp.UUCP> <766@twwells.uucp> <1813@dlvax2.datlog.co.uk> Reply-To: guy@auspex.UUCP (Guy Harris) Organization: Auspex Systems, Santa Clara Lines: 40 >>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.)