From The C Programming Language (Brian W. Kernighan), 2.7 TYPE CONVERSIONS, pg 43 :
“There is one subtle point about the
conversion of characters to integers.
… On some macines a char whose
leftmost bit is 1 will be converted to
a negative integer. On others, … is
always positive. For portability,
specify signed or unsigned if
non-character data is to be stored in
char variables.”
My questions are:
-
Why would anyone want to store
non-char data in char? (an example
where this is necessary will be real
nice) -
Why does integer value of char
change when it is converted to int? -
Can you elaborate more on this
portability issue?
In regards to 1)
People often use char arrays when they really want a byte buffer for a data stream. Its not great practice, but plenty of projects do it, and if you’re careful, no real harm is done. There are probably other times as well.
In regards to 2)
Signed integers are often sign extended when they are moved from a smaller data type. Thus
11111111b (-1 in base 10) becomes 11111111 11111111 11111111 11111111 when expanded to 32 bits. However, if the char was intended to be unsigned +255, then the signed integer may end up being -1.
About portability 3)
Some machines regard chars as signed integers, while others interpret them as unsigned. It could also vary based on compiler implementation. Most of the time you don’t have to worry about it. Kernighan is just trying to help you understand the details.
Edit
I know this is a dead issue, but you can use the following code to check if char’s on your system are signed or unsigned: