Depending on the environment and compiler settings, the type char
can be signed or unsigned by default, which means the range of values for single character constants on 8-bit 2s complement systems can be either -128..127
or 0..255
.
In the ubiquitous ASCII character set, its ISO-8859-X extensions or the UTF-8 encoding, upper- and lowercase letters as well as digits have values below 127.
But such is not the case with the EBCDIC character set:
'A'
is 0xC1, 'a'
is 0x81 and '1'
is 0xF1.
Since these value are above 127, does it mean the type char
must be unsigned on 8-bit EBCDIC systems? Or can 'a'
, 'A'
and '1'
have negative values?
What about other character sets? Can the letters or digits ever have negative values?
Unsigned char must be used for accessing memory as a block of bytes or for small unsigned integers. Signed char must be used for small signed integers and simple char must be used only for ASCII characters and strings.
Negative Numbers. Char is an unsigned type and cannot represent a negative value. In any case, you should not use Char to hold numeric values.
Maximum value of signed char in C++It is generally used to store 8-bit characters. Being a signed data type, it can store positive values as well as negative values. Size of 8 bits is occupied where 1 bit is used to store the sign of the value.
You would use a signed char when you need to represent a quantity in the range [-128, 127] and you can't (for whatever reason) spare more than a single byte to do it.
C99 states that:
6.2.5 Types
An object declared as type char is large enough to store any member of the basic execution character set.
If a member of the basic execution character set is stored in a char its value is guaranteed to be nonnegative.
Thus, if the machine in question uses EBCDIC encoding and 8-bit char
, then the C99 compliant compiler designed for this machine must have plain char
be unsigned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With