[C++] How many char types are there?

Started by
7 comments, last by Zahlman 15 years, 3 months ago
I'll keep this short. Why does the following compile:
void f(char) {}
void f(signed char) {}
void f(unsigned char) {}
while the following does not:
void f(int) {}
void f(signed int) {}
void f(unsigned int) {}
? Thanks in advance.
Advertisement
The signed-ness of char is unspecified, whereas the signed-ness of int is "signed" by default. See 3.9.1 and 7.1.5.2 in the standard.
Yes, but still, a char is either signed or unsigned, so I would expect that the first function would "clash" with either the second or third function; which one being implementation-dependent.

The fact that this code compiles makes it seem like there's a third option for the "signedness" of a char, but that doesn't make any sense... does it?
Quote:Yes, but still, a char is either signed or unsigned

The standard states that there are three types of char:
char
unsigned char
and signed char
Each being distinct of another.

edit: added standard quote 3.9.1.1
Quote:
Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types
Quote:Original post by Gage64
Yes, but still, a char is either signed or unsigned


Quote:Original post by ToohrVyk
See 3.9.1 and 7.1.5.2 in the standard.


char is neither signed nor unsigned.
Thanks for the replies. So it was a third option after all. Seems a bit strange...

BTW, I don't have access to the C++ standard, so I wasn't able to follow ToohrVyk's references.
One more question: VC++ has an option "Default char unsigned". If a char is neither signed nor unsigned, what does that option do?
Quote:Original post by Gage64
One more question: VC++ has an option "Default char unsigned". If a char is neither signed nor unsigned, what does that option do?


Maybe I should have quoted all of the paragraph.

Quote:
Objects declared as characters (char) shall be large enough to store any member of the implementation’s
basic character set. If a character from this set is stored in a character object, the integral value of that character
object is equal to the value of the single character literal form of that character. It is implementation defined whether a char object can hold negative values. Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types. A char, a signed char, and an unsigned char occupy the same amount of storage and have the same alignment requirements (3.9); that is, they have the same object representation. For character types, all bits of the object representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types.
In any particular implementation, a plain char object can take on either the same values as a signed char or an unsigned char; which one is implementation-defined.

Gcc has a similar option using -fsigned-char and -funsigned-char.
Quote:Original post by Gage64
One more question: VC++ has an option "Default char unsigned". If a char is neither signed nor unsigned, what does that option do?


It causes char to behave like unsigned char in that project. In general, it will behave either exactly like unsigned char or exactly like signed char (except for all the implications of it being a distinct type), but the implementation gets to choose which (thought it must be consistent).

This topic is closed to new replies.

Advertisement