tags:

views:

663

answers:

3

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

+9  A: 

The book is wrong. The standard does not specify if plain char is signed or unsigned.

In fact, the standard defines three distinct types: char, signed char, and unsigned char. If you #include <limits.h> and then look at CHAR_MIN, you can find out if plain char is signed or unsigned (if CHAR_MIN is less than 0 or equal to 0), but even then, the three types are distinct as far as the standard is concerned.

Alok
@Alok: the same is not true for some other datatypes, for example `int` means `signed int` always, right? Apart from `char`, what other datatypes have the same confusion in `C`?
Lazer
@eSKay: yes, `char` is the only type that can be signed or unsigned. `int` is equivalent to `signed int` for example.
Alok
+4  A: 

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. You can also explicitly ask for signed characters with -fsigned-char.

On MSVC, the default is signed but you can modify that with /J.

R Samuel Klatchko
Interesting that Schildt's description doesn't match MSVC's behavior since his books are usually geared toward MSVC users. I wonder if MS changed the default at some point?
Michael Burr
+3  A: 

The standard has this to say about the signed-ness of type char:

The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.

and in a footnote:

CHAR_MIN, defined in <limits.h>, will have one of the values 0 or SCHAR_MIN, and this can be used to distinguish the two options. Irrespective of the choice made, char is a separate type from the other two and is not compatible with either.

Michael Burr