In the book "Complete Reference of C" it is mentioned that char
is by default unsigned.
But I am trying to verify this with GCC as well as Visual Studio. It is taking it as signed by default.
Which one is correct?
In the book "Complete Reference of C" it is mentioned that char
is by default unsigned.
But I am trying to verify this with GCC as well as Visual Studio. It is taking it as signed by default.
Which one is correct?
According to The C Programming Language book by Dennis Ritchie which is the de-facto standard book for ANSI C, plain chars either signed or unsigned are machine dependent, but printable characters are always positive.
According to the C standard the signedness of plain char is "implementation defined".
In general implementors chose whichever was more efficient to implement on their architecture. On x86 systems char is generally signed. On arm systems it is generally unsigned (Apple iOS is an exception).
C99 N1256 draft 6.2.5/15 "Types" has this to say about the signed-ness of type
char
:and in a footnote:
As Alok points out, the standard leaves that up to the implementation.
For gcc, the default is signed, but you can modify that with
-funsigned-char
. note: for gcc in Android NDK, the default is unsigned. You can also explicitly ask for signed characters with-fsigned-char
.On MSVC, the default is signed but you can modify that with
/J
.According to "The C++ Programming Language" by Bjarne Stroustrup,
char
is "implementation defined". It can besigned char
orunsigned char
depending on implementation. You can check whetherchar
is signed or not by usingstd::numeric_limits<char>::is_signed
.Now, we known the standard leaves that up to the implementation.
But how to check a type is
signed
orunsigned
, such aschar
?I wrote a macro to do this:
#define IS_UNSIGNED(t) ((t)~1 > 0)
and test it with
gcc
,clang
, andcl
. But I do not sure it's always safe for other cases.