tags:

views:

85

answers:

8
+1  A: 

Computers do not "understand" the concept of alphabets or characters; they only work on numbers. So a bunch of people got together and agreed on what number maps to what letter. The most common one in use is ASCII (although the language does not guarantee that).

In ASCII, the letter A has the code 65. In environments using ASCII, the letter A would be represented by the number 65.

The char datatype also serves as an integral type - meaning that it can hold just numbers, so unsigned and signed was allowed. On most platforms I've seen, char is a single 8-bit byte.

In silico
how does this explain unsigned and signed?
Ronny
Because signedness is a relevant concept for numbers.
dan04
A: 

You can use the the overflow from 255 to 0? (I don't know. Just a guess)

Maybe it is not only about characters but also about numbers between -128 and 127, and 0 to 255.

Ronny
+2  A: 

A char is actually an integral type. It is just that the type is also used to represent a character too. Since it is an integral type, it is valid to talk about signedness.

(I don't know exactly about the historical reason. Probably to save a keyword for byte by conflating it with char.)

KennyTM
It doesn't directly follow from the text. Do you have a source? I understand the argument and it's probably correct. Just to be sure?!
Ronny
@Ronny: §3.9.1/2 and /3?
KennyTM
A: 

Think of the ASCII character set.

Historically, all characters used for text in computing were defined by the ASCII character set. Each character was represented by an 8 bit byte, which was unsigned, hence each character had a value in the range of 0 - 255.

The word character was reduced to char for coding.

An 8 bit char used the same memory as an 8 bit byte and as such they were interchangeable as far as a compiler was concerned.

The compiler directive unsigned (all numbers were signed by default as twos compliment is used to represent negative numbers in binary) when applied to a byte or a char forced them to have a value in the range 0-255.

If unsigned then then had a value of -128 - +127.

Nowadays with the advent of UNICODE and multiple byte character sets this relationship between byte and char no longer exists.

ChrisBD
Actually, the confusion can be traced to the fact that bytes are 8 bits, but ASCII defines only [0-127]. Both the ranges [-128, 127] and [0,255] are supersets of ASCII. Therefore you can say that ASCII left open both options.
MSalters
True - I was thinking an extended version such as the old MS-DOS code page 437.
ChrisBD
A: 

I am not sure, but my guess is that signed character was useful to distinguis between "regular" ASCII (which only takes 7 bits) and the "other" characters. if (ch > 0) was true, you knew that you were dealing with one of the familiar characters, printed the same way on all terminals. When (ch < 0), it was an "OEM code page" or some such, which means that the actual character printed depended on the specific terminal's encoding. It could produce table-drawing lines, Cyrillic letters or Scandinavian accents.

Arkadiy
7-bit character sets were common in the 1970s, so the designers of C didn't think much about character values 128-255.
dan04
"printed the same way on all terminals" certainly was not true either. IBM terminals with EBCDIC will not print A for ch==65.
MSalters
+1  A: 

In C (and thus C++), char does not mean character. It means a byte (int_least8_t). This is a historical legacy from the pre-Unicode days when a characters could actually fit in a char, but is now a flaw in the language.

Since char is really a small integer, having signed char and unsigned char makes sense. There are actually three distinct char types: char, signed char, and unsigned char. A common convention is that unsigned char represents bytes while plain char represents characters UTF-8 code units.

dan04
+1  A: 

You're reading too much in to it. A character is a small integral type that can hold a character. End of story. Unsigned char was never introduced or intended, it's just how it is, because char is an integral type identical to int or long or short, it's just the size that's different. The fact is that there's little reason to use unsigned char, but people do if they want one-byte unsigned integral storage.

DeadMG
A `char` **was** a small integral type that could hold a character.
dan04
A: 

If you want a small memory foot print and want to store a number than signed and unsigned char are usefull.

unsigned char is needed if you want to use a value between 128-255

unsigned char score = 232;

signed char is usfull if you want to store the difference between two characters.

signed char diff = 'D' - 'A';

char is distinct from the other two because you can not assume it is either.

Martin York