tags:

views:

207

answers:

5

Is an int the same type as unsigned or signed?

+12  A: 

Plain int is the same as signed is the same as signed int

Anthony Williams
Some (all?) compilers have a switch to change the default to unsigned. In MSVC, it's /J.
Graeme Perrow
FYI The CCS compiler for Microchip PIC controllers is unsigned by default. Argh!
kenny
The /J compiler switch affects char, not int. "char" may or may not be signed. "char", "signed char" and "unsigned char" are always distinct types, whether or not "char" is signed.
Anthony Williams
+2  A: 

ints are signed by default.

cHao
+11  A: 

C++ Standard 3.9.1/2:

There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” <...>

C++ Standard 3.9.1/3:

For each of the signed integer types, there exists a corresponding (but different) unsigned integer type: “unsigned char”, “unsigned short int”, “unsigned int”, and “unsigned long int,” <...>

So, sizeof(int) is equal to sizeof(unsigned). But boost::is_same< int, unsigned >::value is false.

Kirill V. Lyadvinsky
+1 for citing Standard.
Cumbayah
+1  A: 

signed int is the same as int and specifies an integer value that can have both positive and negative values.

unsigned int on the other hand can only have positive values, so the greatest positive value is much larger than that of a signed int.

the_void
Actually the *range* of values is exactly the same size, it's just shifted so that the minimum value is 0.
Graeme Perrow
You're right, that was a poor choice of words. Fixed ;)
the_void
+2  A: 

ints are signed by default, as are longs.

So, int, signed and signed int are the same thing.

Similarly long and signed long are the same.

chars on the other hand, don't have a default. Implementations can consider them signed or unsigned (many have a command line switch to choose). char, signed char and unsigned char are considered three distinct types for overload resolution, template instaniation and other places.

James Curran
The last sentence is too restricted. It's not just overload resolution, it's everywhere. E.g. also as template arguments. Thus `std::vector<int>` is the same type as `std::vector<signed int>`, but `std::vector<char>` is **not** the same type as `std::vector<signed char>`
MSalters
@MSalters: Good point, updated.
James Curran