Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative ASCII value

Tags:

c++

c

What's the point of negative ASCII values?

int a = '«'; //a = -85 but as in ASCII table '<<' should be 174
like image 220
user963241 Avatar asked Jan 14 '11 11:01

user963241


People also ask

Can a char be negative?

The signed char type can store , negative , zero , and positive integer values . It has a minimum range between -127 and 127 , as defined by the C standard .

Can char hold negative value?

Char is an unsigned type and cannot represent a negative value. In any case, you should not use Char to hold numeric values.

What ASCII 32?

ASCII code 32 = space ( Space ) ASCII code 33 = ! ( ASCII code 34 = " ( Double quotes ; Quotation mark ; speech marks ) ASCII code 35 = # ( Number sign ) ASCII code 36 = $ ( Dollar sign )


1 Answers

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. :)

like image 100
Lightness Races in Orbit Avatar answered Oct 06 '22 11:10

Lightness Races in Orbit