Reputation: 26888
Is int
by default signed long int
in C++?
Is it platform and/or compiler dependent? If so, how?
[EDIT]
Are any of the following guaranteed to be duplicate?
signed short int
signed int
signed long int
signed long long int
unsigned short int
unsigned int
unsigned long int
unsigned long long int
Upvotes: 13
Views: 19953
Reputation: 153820
All of the integer types are different, i.e. you can safely overload functions for all of them and you won't get any conflict. However, some times use the same number of bits for their representation. Even if they use the same number of bits signed and unsigned types always have a different range. Except for char
, using any integer type without signed
is equivalent to using it with signed
, i.e. signed int
and int
are equivalent. char
is a different type as signed char
and unsigned char
but char
has the same representation and range of either signed char
or unsigned char
. You can use std::numeric_limits<char>::is_signed
to find out which it uses.
On to the more interesting aspects. The following conditions are all true:
7 <= std::numeric_limits<signed char>::digits
sizeof(char) == 1
sizeof(char) == sizeof(signed char)
sizeof(char) == sizeof(unsigned char)
15 <= std::numeric_limits<short>::digits
sizeof(char) <= sizeof(short)
sizeof(short) <= sizeof(int)
31 <= std::numeric_limits<long>::digits
sizeof(int) <= sizeof(long)
63 <= std::numeric_limits<long long>::digits
sizeof(long) <= sizeof(long long)
sizeof(X) == sizeof(signed X)
sizeof(signed X) == sizeof(unsigned X)
(where "X" is one of char
, short
, int
, long
, and long long
).
This means that the size of all integer types can be the same as long as this types hold at least 64 bits (and apparently the Cray X-MP was such a beast). On contemporary machines typically sizeof(int) == sizeof(long)
but there are machines where sizeof(int) == sizeof(short)
. Whether long
is 32 or 64 bits depends on the actual architecture and both kinds are currently around.
Upvotes: 14
Reputation: 283624
plain int
is signed, whether or not it's the same size as long int
is platform-dependent.
What's guaranteed is that
sizeof (int) <= sizeof (long)
and int
is big enough to hold at least all values from -32767 to 32767.
What the standard says: (section [basic.fundamental]
:
There are five standard signed integer types :
signed char
,short int
,int
,long int
, andlong long int
. In this list, each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types. Plainint
s have the natural size suggested by the architecture of the execution environment; the other signed integer types are provided to meet special needs.
Upvotes: 25
Reputation: 506905
The long
and short
modifiers are not exactly like signed
and unsigned
. The latter two can be put on any integer type, but if you leave them off, then signed
is the default for each integer type (except char
). So int
and signed int
are the same type.
For long
and short
, if you leave them off, neither is chosen, but the resulting type is different. long int
, short int
and int
are all different types, with short int
<= int
<= long int
.
The int
after long
, short
, signed
and unsigned
is optional: signed int
and signed
are the same type.
Upvotes: 5
Reputation: 3918
In C++ int
is signed int
by default, so there is no problem with that. However, int
and long int
are different types in C++, so this is not the same from the point of view of the language. Implementation of int
and long int
is platform/compiler specific - they are both integral types which might be identical. The only limitation C++ standard imposes is that sizeof( long int ) >= sizeof( int )
.
Upvotes: 2
Reputation: 210402
signed
and int
are both the same as signed int
by default.
Neither is the same type as signed short int
or signed long int
.
Upvotes: 1
Reputation: 86506
Plain int
is equivalent to signed int
. That much is standard. Anything past that is not guaranteed; int
and long
are different types, even if your particular compiler makes them the same size. The only guarantee you have is that a long
is at least as big as an int
.
Upvotes: 8