What's the point of negative ASCII values?
int a = '«'; //a = -85 but as in ASCII table '<<' should be 174
There are no negative ASCII values. ASCII includes definitions for 128 characters. Their indexes are all positive (or zero!).
You're seeing this negative value because the character is from an Extended ASCII set and is too large to fit into the char literal. The value therefore overflows into the bit of your char
(signed on your system, apparently) that defines negativeness.
The workaround is to write the value directly:
unsigned char a = 0xAE; // «
I've written it in hexadecimal notation for convention and because I think it looks prettier than 174
. :)
This is an artefact of your compiler's char
type being a signed integer type, and int
being a wider signed integer type, and thus the character constant is considered a negative number and is sign-extended to the wider integer type.
There is not much sense in it, it just happens. The C standard allows for compiler implementations to choose whether they consider char
to be signed or unsigned. Some compilers even have compile time switches to change the default. If you want to make sure about the signedness of the char
type, explicitly write signed char
or unsigned char
, respectively.
Use an unsigned char
to be extended to an int
to avoid the negative int
value, or open a whole new Pandora's box and enjoy wchar
.
int
even comes into the picture. The literal ends up with the value -85 due to overflow, and then this value -85 is assigned to the int. (It's possible that this is what you meant, and I misunderstood you.)
Jan 14, 2011 at 11:42
char
. To make it not fit, you need multiple characters (int a = 'ab'; /* !! */
). C does not require that characters have positive values. Hence, implementations are free to assign the value -85 to '«'
There is no such thing. ASCII is a table of characters, each character has an index, or a position, in the table. There are no "negative" indices.
Some compilers, though, consider char
to be a signed integral data type, which is probably the reason for the confusion here.
If you print it as unsigned int
, you will get the same bits interpreted as a unsigned (positive) value.
ASCII ranges 0..127, ANSI (also called 'extended ASCII') ranges 0..255.
ANSI range won't fit in a signed char (the default type for characters in most compilers).
Most compilers have an option like 'char' Type is Unsigned (GCC).
char
type is completely implementation-specified. Character literals have type char
. His system just happens to use a signed implementation for char
(as do the majority of popular systems, I believe).
Jan 14, 2011 at 11:40
int
, which is explicitly, always, the same as signed int
.)
Jan 14, 2011 at 11:40
char
, char
is a different type from both signed char
and unsigned char
.
char
on most implementations that you find on popular commodity hardware nowadays is a signed type.
Jan 14, 2011 at 17:29
I had this artifact. When you use char as symbols you have no problem. But when you use it as integer (with isalpha(), etc.) and the ASCII code is greater then 127, then the 'char' interpret as 'signed char' and isalpha() return an exception. When I need use the 'char' as integer I cast the 'char' to unsigned:
@n0rd: koi8 codepage uses ascii from 128 to 255 and other national codepages: http://www.asciitable.com/
In a character representation, you have 8 bits (1 byte) allotted.
Out of this, the first bit is used to represent sign. In the case of unsigned character, it uses all 8 bits to represent a number allowing 0 to 255 where
128-255 are called extended ASCII.
Due to the representation in the memory as I have described, we have -1 having the same value as 255, char(-2)==char(254)
a = -85
is incorrect. It should bea = -82
. This is because the range of the extended ASCII table is 0 - 255 inclusive. That's a total of 256 possible values. For both a positive value and a negative value to be the same symbol (in this case<<
) they have to be 256 values apart for the symbol to repeat itself.