What's the point of negative ASCII values?
int a = '«'; //a = -85 but as in ASCII table '<<' should be 174
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
.
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).
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)