[ACCEPTED]-char!=(signed char), char!=(unsigned char)-char
Here is your answer from the standard:
3.9.1 22 Fundamental types [basic.fundamental]
Objects 21 declared as characters (
char
) shall be large 20 enough to store any member of the implementation's 19 basic character set. If a character from 18 this set is stored in a character object, the 17 integral value of that character object 16 is equal to the value of the single character 15 literal form of that character. It is implementation-defined 14 whether achar
object can hold negative values. Characters 13 can be explicitly declaredunsigned
orsigned
. Plainchar
,signed char
, andunsigned char
are three distinct types. Achar
, a 12signed char
, and anunsigned char
occupy the same amount of storage 11 and have the same alignment requirements 10 (basic.types); that is, they have the same object representation. For 9 character types, all bits of the object 8 representation participate in the value 7 representation. For unsigned character 6 types, all possible bit patterns of the 5 value representation represent numbers. These 4 requirements do not hold for other types. In 3 any particular implementation, a plain 2char
object can take on either the same values 1 as asigned char
or anunsigned char
; which one is implementation-defined.
While most integral types like short
and int
default 6 to being signed
, char
does not have a default signage 5 in C++.
It is neither the type signed char
nor unsigned char
, so 4 implementations may decide whether it is 3 signed.
It's a common mistake that C++ programmers 2 run into when they use char
as an 8 bit integer 1 type.
For questions such as this, i like to look 14 into the Rationale document for C, which 13 often provides answers to C++ mysteries 12 as well, that sometimes arise for me when 11 reading the Standard. It has this to say 10 about it:
Three types of char are specified: signed, plain, and 9 unsigned. A plain char may be represented 8 as either signed or unsigned, depending 7 upon the implementation, as in prior practice. The 6 type signed char was introduced to make 5 available a one-byte signed integer type 4 on those systems which implement plain char 3 as unsigned. For reasons of symmetry, the 2 keyword signed is allowed as part of the 1 type name of other integral types.
that's correct, char
, unsigned char
and signed char
are separate types. It 4 probably would have been nice if char
was just 3 a synonym for either signed char
or unsigned char
depending on your 2 compilers implementation, but the standard 1 says they are separate types.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.