I\'m looking for detailed information regarding the size of basic C++ types. I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler.
The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold. You can infer minimum size in bits from the required range. You can infer minimum size in bytes from that and the value of the CHAR_BIT
macro that defines the number of bits in a byte. In all but the most obscure platforms it's 8, and it can't be less than 8.
One additional constraint for char
is that its size is always 1 byte, or CHAR_BIT
bits (hence the name). This is stated explicitly in the standard.
The C standard is a normative reference for the C++ standard, so even though it doesn't state these requirements explicitly, C++ requires the minimum ranges required by the C standard (page 22), which are the same as those from Data Type Ranges on MSDN:
signed char
: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement and sign-and-magnitude platforms)unsigned char
: 0 to 255char
: same range as signed char
or unsigned char
, implementation-definedsigned short
: -32767 to 32767unsigned short
: 0 to 65535signed int
: -32767 to 32767unsigned int
: 0 to 65535signed long
: -2147483647 to 2147483647unsigned long
: 0 to 4294967295signed long long
: -9223372036854775807 to 9223372036854775807unsigned long long
: 0 to 18446744073709551615A C++ (or C) implementation can define the size of a type in bytes sizeof(type)
to any value, as long as
sizeof(type) * CHAR_BIT
evaluates to a number of bits high enough to contain required ranges, andsizeof(int) <= sizeof(long)
).Putting this all together, we are guaranteed that:
char
, signed char
, and unsigned char
are at least 8 bitssigned short
, unsigned short
, signed int
, and unsigned int
are at least 16 bitssigned long
and unsigned long
are at least 32 bitssigned long long
and unsigned long long
are at least 64 bitsNo guarantee is made about the size of float
or double
except that double
provides at least as much precision as float
.
The actual implementation-specific ranges can be found in
header in C, or
in C++ (or even better, templated std::numeric_limits
in
header).
For example, this is how you will find maximum range for int
:
C:
#include
const int min_int = INT_MIN;
const int max_int = INT_MAX;
C++:
#include
const int min_int = std::numeric_limits::min();
const int max_int = std::numeric_limits::max();