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.
But are there any standards for C++?
I'm using Visual Studio 2008 on a 32-bit architecture. Here is what I get:
char : 1 byte short : 2 bytes int : 4 bytes long : 4 bytes float : 4 bytes double: 8 bytes
I tried to find, without much success, reliable information stating the sizes of
float (and other types I didn't think of) under different architectures and compilers.
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).
Minimum ranges required by the standard (page 22) are:
and 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 255
char: same range as
unsigned char, implementation-defined
signed short: -32767 to 32767
unsigned short: 0 to 65535
signed int: -32767 to 32767
unsigned int: 0 to 65535
signed long: -2147483647 to 2147483647
unsigned long: 0 to 4294967295
signed long long: -9223372036854775807 to 9223372036854775807
unsigned long long: 0 to 18446744073709551615
A C++ (or C) implementation can define the size of a type in bytes
sizeof(type) to any value, as long as
sizeof(type) * CHAR_BITevaluates to a number of bits high enough to contain required ranges, and
sizeof(int) <= sizeof(long)).
The actual implementation-specific ranges can be found in
<limits.h> header in C, or
<climits> in C++ (or even better, templated
For example, this is how you will find maximum range for
#include <limits.h> const int min_int = INT_MIN; const int max_int = INT_MAX;
#include <limits> const int min_int = std::numeric_limits<int>::min(); const int max_int = std::numeric_limits<int>::max();