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
An int type in C, C++, and C# is signed by default. If negative numbers are involved, the int must be signed; an unsigned int cannot represent a negative number.
An int is signed by default, meaning it can represent both positive and negative values. An unsigned is an integer that can never be negative. If you take an unsigned 0 and subtract 1 from it, the result wraps around, leaving a very large number (2^32-1 with the typical 32-bit integer size).
Notes. Whilst most types are signed by default (short, int, long long), char is unsigned by default.
By default, integers are signed, which means the number's sign is stored as part of the number (using a single bit called the sign bit). Therefore, a signed integer can hold both positive and negative numbers (and 0).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With