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.
Updated: C++11 brought the types from TR1 officially into the standard:
And the "sized" types from <cstdint>
Plus you get:
These types represent the smallest integer types with at least the specified number of bits. Likewise there are the "fastest" integer types with at least the specified number of bits:
What "fast" means, if anything, is up to the implementation. It need not be the fastest for all purposes either.
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."
#include <iostream>
int main ( int argc, char * argv[] )
{
std::cout<< "size of char: " << sizeof (char) << std::endl;
std::cout<< "size of short: " << sizeof (short) << std::endl;
std::cout<< "size of int: " << sizeof (int) << std::endl;
std::cout<< "size of long: " << sizeof (long) << std::endl;
std::cout<< "size of long long: " << sizeof (long long) << std::endl;
std::cout<< "size of float: " << sizeof (float) << std::endl;
std::cout<< "size of double: " << sizeof (double) << std::endl;
std::cout<< "size of pointer: " << sizeof (int *) << std::endl;
}
size of char: 1
size of short: 2
size of int: 4
size of long: 8
size of long long: 8
size of float: 4
size of double: 8
size of pointer: 8
I notice that all the other answers here have focused almost exclusively on integral types, while the questioner also asked about floating-points.
I don't think the C++ standard requires it, but compilers for the most common platforms these days generally follow the IEEE754 standard for their floating-point numbers. This standard specifies four types of binary floating-point (as well as some BCD formats, which I've never seen support for in C++ compilers):
How does this map onto C++ types, then? Generally the float
uses single precision; thus, sizeof(float) = 4
. Then double
uses double precision (I believe that's the source of the name double
), and long double
may be either double or quadruple precision (it's quadruple on my system, but on 32-bit systems it may be double). I don't know of any compilers that offer half precision floating-points.
In summary, this is the usual:
sizeof(float)
= 4sizeof(double)
= 8sizeof(long double)
= 8 or 16As you mentioned - it largely depends upon the compiler and the platform. For this, check the ANSI standard, http://home.att.net/~jackklein/c/inttypes.html
Here is the one for the Microsoft compiler: Data Type Ranges.
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 <limits.h>
header in C, or <climits>
in C++ (or even better, templated std::numeric_limits
in <limits>
header).
For example, this is how you will find maximum range for int
:
C:
#include <limits.h>
const int min_int = INT_MIN;
const int max_int = INT_MAX;
C++:
#include <limits>
const int min_int = std::numeric_limits<int>::min();
const int max_int = std::numeric_limits<int>::max();
In practice there's no such thing. Often you can expect std::size_t
to represent the unsigned native integer size on current architecture. i.e. 16-bit, 32-bit or 64-bit but it isn't always the case as pointed out in the comments to this answer.
As far as all the other built-in types go, it really depends on the compiler. Here's two excerpts taken from the current working draft of the latest C++ standard:
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.
For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: unsigned char, unsigned short int, unsigned int, unsigned long int, and unsigned long long int, each of which occupies the same amount of storage and has the same alignment requirements.
If you want to you can statically (compile-time) assert the sizeof these fundamental types. It will alert people to think about porting your code if the sizeof assumptions change.