Why can a char be signed?

The signed keyword indicates a value that can be both negative and positive like a negative and positive number. For example, 1 byte represented as a signed integer can store a range from -128 to 127. However, with characters, there's no concept of "negative". So why can char data type be signed?
ASCII codes are 0..127. If you store such code in a byte, which can hold 0..255 or -128..127, why choose one over another?
However, with characters, there's no concept of "negative"
char does not store "characters", it stores their representation in form of 8bit integer.

ASCII codes are 0..127
And there is extended ASCII which often used when dealing with another languages. From there comes ugliest thing known as codepages.
why can char data type be signed?

As MiiNiPass has already said, chars are stored as 8-bit integers. And, as with other int types, there is both a signed and an unsigned versions.

Part of the answer is that the ASCII desgners used a few bits as they could get away with, which was 7, leaving a parity bit which could be used for for error checking. This means that that both signed and unsigned 8-bit ints were able to store the required range of (positive) values.

The other part of the answer is that the C standard does not mandate which should be used in the early days, so compiler implementors were free to chose the type their processor architecture could handle the quickest, which is (e.g.) signed for Intel x86 but unsigned for ARM.

The standard has been amended, but it now says that the default char type can be signed on unsigned.

Andy
Last edited on
Isn't char also distinct from its signed and unsigned forms?
Isn't char also distinct from its signed and unsigned forms?

Yes, in the sense that it another type as far as the C (and hence C++) language is concerned.

But it still has to be either a signed or an unsigned 8-bit integral type. So char is either equivalent to a signed or an unsigned char.

Andy
I know, I just wanted to point out that it was a distinct type.
Topic archived. No new replies allowed.