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

TypeSize
64-bit32-bit16-bit
char8 bits8 bits8 bits
short16 bits16 bits16 bits
int32 bits32 bits16 bits
long32 bits32 bits32 bits
long long64 bits64 bits
ptrdiff_t / size_t64 bits32 bits16 bits
int8_t / uint8_t8 bits8 bits8 bits
int16_t / uint16_t16 bits16 bits16 bits
int32_t / uint32_t32 bits32 bits32 bits
int64_t / uint64_t64 bits64 bits64 bits