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 char
, short
, int
, long
, double
, float
(and other types I didn't think of) under different architectures and compilers.
The C++ Standard says it like this:
3.9.1, §2:
The conclusion: It depends on which architecture you're working on. Any other assumption is false.
1) Table N1 in article "The forgotten problems of 64-bit programs development"
2) "Data model"
Nope, there is no standard for type sizes. Standard only requires that:
The best thing you can do if you want variables of a fixed sizes is to use macros like this:
Then you can use WORD to define your variables. It's not that I like this but it's the most portable way.
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, orCHAR_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 255char
: same range assigned char
orunsigned 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 assizeof(type) * CHAR_BIT
evaluates to a number of bits high enough to contain required ranges, andsizeof(int) <= sizeof(long)
).The actual implementation-specific ranges can be found in
<limits.h>
header in C, or<climits>
in C++ (or even better, templatedstd::numeric_limits
in<limits>
header).For example, this is how you will find maximum range for
int
:C:
C++:
There is a standard and it is specified in the various standards documents (ISO, ANSI and whatnot).
Wikipedia has a great page explaining the various types and the max they may store: Integer in Computer Science.
However even with a standard C++ compiler you can find out relatively easily using the following code snippet:
Documentation for std::numeric_limits can be found at Roguewave. It includes a plethora of other commands you can call to find out the various limits. This can be used with any arbitrary type that conveys size, for example std::streamsize.
John's answer contains the best description, as those are guaranteed to hold. No matter what platform you are on, there is another good page that goes into more detail as to how many bits each type MUST contain: int types, which are defined in the standard.
I hope this helps!
When it comes to built in types for different architectures and different compilers just run the following code on your architecture with your compiler to see what it outputs. Below shows my Ubuntu 13.04 (Raring Ringtail) 64 bit g++4.7.3 output. Also please note what was answered below which is why the output is ordered as such:
"There are five standard signed integer types: signed char, short int, int, long int, and long long int. In this list, each type provides at least as much storage as those preceding it in the list."