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|
||unicode 16 character||16||
||unicode 32 character||32||
||single-precision floating-point||6 significant digits||
||double-precision floating-point||10 significant digits||none|
||extended-precision floating-point||10 significant digits||
Note: type 'long long' was introduced in c++11
literal types notes
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).
u8 is also used to specify utf-8 strings, e.g.
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 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 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 (
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.
char32_t are intended for unicode characters (a standard to represent any character used in essentially any natural language).
<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