C++ types
C++ has several standard, or built-in, types. They could be divided into several classes: integers, character and floating point numbers.
Integers
Integer types differ by their size in bits. There are 8 bits (char
),
16 bits (short
), 32 bits (int
), and 64 bits (long long
).
Though the exact size depends on a processor architecture and compiler;
in order to get the same size in all cases, one should use constant size types, like
int32_t
, defined in inttypes.h
(cinttypes
in C++) header.
By default, each integer type is signed; to make them unsigned they should be prefixed by
unsigned
keyword.
The most common integer type in int
it is 32 bits long (except for 16-bit processors,
where it is 16 bits long; on 64-bit processors it is 32 bits long). You should remember
that on 64-bit processors it is less than the pointer size (which is 64 bits), thus it should not be casted.
In modern style programs, it is recommend to use size_t
instead of unsigned int
and ptrdif_t
instead of int
, these types are 32 bits long on 32-bit processor
and or 64 bits long on 64-bit processors. This will allow to avoid unsafe cast as well as will not
downgrade the perfomance on both modern and old processors.
The next type is long
, on all processors it is 32 bits long including 16 bits processor
(unlike int
). It is not recommended for usage in modern programs (use int
instead).
Type tables
Type | Size | ||
---|---|---|---|
64-bit | 32-bit | 16-bit | |
char | 8 bits | 8 bits | 8 bits |
short | 16 bits | 16 bits | 16 bits |
int | 32 bits | 32 bits | 16 bits |
long | 32 bits | 32 bits | 32 bits |
long long | 64 bits | 64 bits | |
ptrdiff_t / size_t | 64 bits | 32 bits | 16 bits |
int8_t / uint8_t | 8 bits | 8 bits | 8 bits |
int16_t / uint16_t | 16 bits | 16 bits | 16 bits |
int32_t / uint32_t | 32 bits | 32 bits | 32 bits |
int64_t / uint64_t | 64 bits | 64 bits | 64 bits |