C++ arithmetic types
Arithmetic types are divided in two catogories:
- Integral types (which include character and boolean types).
- Floating-point types.
The size of this arithmetic types varies across machines, but the standard guarantees minimum size as listed in the table below.
Compilers are allowed to use larger sizes, so the largest value that a type can represent also varies.
Type | Meaning | Minimum size (bits) | Suffix/prefix for constants |
---|---|---|---|
bool |
boolean | n/a | n/a |
char |
character | 8 | n/a |
wchar_t |
wide character | 16 | L (prefix) |
char16_t |
unicode 16 character | 16 | u (prefix) |
char32_t |
unicode 32 character | 32 | U (prefix) |
short |
short integer | 16 | n/a |
int |
integer | 16 | none |
long |
long integer | 32 | l or L (suffix) |
long long |
long integer | 64 | ll or LL (suffix) |
float |
single-precision floating-point | 6 significant digits | f or F (suffix) |
double |
double-precision floating-point | 10 significant digits | none |
long double |
extended-precision floating-point | 10 significant digits | l or L (suffix) |
Note: type 'long long' was introduced in c++11
literal types notes
The suffix u
or U
for constants can be added to specify the unsigned variant of that type, e.g. u
(unsigned int), ul
(unsigned long), ull
(unsigned long long).
The prefix u8
is also used to specify utf-8 strings, e.g. u8"Hello world!"
.
Some examples:
L'a' // wide character
u8"hello world" // utf-8 string
42ull // unsigned long long
1.2f // float
3.14159L // long double
Note: I personally prefer to put the suffixes in lower case except for 'L', which could be seen as a '1' in lower case.
C++23 also intreduces z
and Z
suffixes for size_t
literals (size_t
can store the maximum size of a theoretically possible object of any type, including array, commonly used for array indexing and loop counting because in 64-bits systems unsigned int
will fail when index exceeds UINT_MAX
or if it relies in 32-bit modular arithmetic).
char notes
A char
is guaranteed to be big enought to hold numeric values corresponding to the characters in the machinen's basic character set, a char
is the same size as a single machine byte.
Other character types (wchar_t
, char16_t
and char32_t
) are used for extended character sets, wchar_t
is guaranteed to be large enought to hold any character in the machine's largest extended character set. char16_t
and char32_t
are intended for unicode characters (a standard to represent any character used in essentially any natural language).
int notes
The header <cstdint>
(<stdint.h
in C and pre-c++11) defines a set of integral types aliases with specific width requirements, along with macros specifying limits and macro functions to create values of these types:
- Cppreference - header cstdint