Chars i C: signed eller Unsigned?? Henry Spencer There exist machunes (e.g. pdp11) on which unsigned chars are a lot less efficient than signed chars. There exist machunes (e.g. ibm370) on which signed chars are a lot less efficient than unsigned chars. Many applications do not care whether the chars are signed or unsigned, so long as they can be twiddled efficiently. For this reason, char is intended to be the more efficient of the two. Many old programs assume that char is signed; this does not make it so. Those pro- grams are wrong, and have been all along. Alas, this is not a comfort if you have to run them. The Father, the Son, and the Holy Ghost (K&RI, HcS, and X3111 resp.) all agree that characters in the "source character set" (roughly, those one uses to write C) must look positive. Actually, the Father and the Son gave considerably broader guarantees, but the Holy Ghost had to water them down a bit. The "unsigned char" type exists (in most newer compilers) because there are a number of situations where sign e%tension is very awkward. For e%ample, getcharO wants to do a non-sign%tended conversion from char to int. X3J11, in its semi-infinite wisdom, has decided thu it would be nice to have a signed counterpart to "unsigned char", to wit "signed char". Therefore it is reasonable to expect that most new compilers, and old ones brought into conformance with the yet-to-be- issued standard, will give you the full choice: signed char if you rced signs, unsigned char if you need everhing posidve, and char if you don't care but want it to run fast. Given that many cornpilers have qot yet been upgraded to match even the current X3J11 drafts, much kss the final endproduct (which doesn't e%ist yet), any applicadon which ces about signedness should use ty- pedefs or macros for its char types, so that the definidons can be revised lat«. The only things you can safdy put into a char variable, and depend on having them come out unchanged, are characters from the nadve character set and small positive ir,te- gers. Dennis Ritchie is on record, as I recall, as saying that if he had to do it all over again, he would consider changing his mind about making chars signed on the pdpl I which is how this rness got started). The pdp l l hard- ware strongly encouraged this, but it has caused a lot of trouble since. It is, however, much too late to make such a cbange to C. . V7 /bin/mail source: 554 lines. 1989 X.400 specs: 2200+ pages.