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.
On a 64-bit machine:
If you are interested in a pure C++ solution, I made use of templates and only C++ standard code to define types at compile time based on their bit size. This make the solution portable across compilers.
The idea behind is very simple: Create a list containing types char, int, short, long, long long (signed and unsigned versions) and the scan the list and by the use of numeric_limits template select the type with given size.
Including this header you got 8 type stdtype::int8, stdtype::int16, stdtype::int32, stdtype::int64, stdtype::uint8, stdtype::uint16, stdtype::uint32, stdtype::uint64.
If some type cannot be represented it will be evaluated to stdtype::null_type also declared in that header.
THE CODE BELOW IS GIVEN WITHOUT WARRANTY, PLEASE DOUBLE CHECK IT.
I'M NEW AT METAPROGRAMMING TOO, FEEL FREE TO EDIT AND CORRECT THIS CODE.
Tested with DevC++ (so a gcc version around 3.5)
There is standard.
C90 standard requires that
C99 standard requires that
Here is the C99 specifications. Page 22 details sizes of different integral types.
Here is the int type sizes (bits) for Windows platforms:
If you are concerned with portability, or you want the name of the type reflects the size, you can look at the header
<inttypes.h>
, where the following macros are available:int8_t
is guaranteed to be 8 bits, andint16_t
is guaranteed to be 16 bits, etc.where
X
is achar
,int
,long
etc.. will give you size ofX
in bits.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
. Thendouble
uses double precision (I believe that's the source of the namedouble
), andlong 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 16There are four types of integers based on size: