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

This topic is 3330 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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) {}

Share on other sites
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.

Share on other sites
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?

Share on other sites
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.

Quote:
 Characters can be explicitly declared unsigned or signed. Plain char, signed char, and unsigned char are three distinct types

Share on other sites
Quote:
 Original post by Gage64Yes, but still, a char is either signed or unsigned

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

char is neither signed nor unsigned.

Share on other sites
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.

Share on other sites
One more question: VC++ has an option "Default char unsigned". If a char is neither signed nor unsigned, what does that option do?

Share on other sites
Quote:
 Original post by Gage64One 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’sbasic character set. If a character from this set is stored in a character object, the integral value of that characterobject 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.

Share on other sites
Quote:
 Original post by Gage64One 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).