Types of literal
Digit separator can be used in numeric literals (floating points and numbers) : 23'456'789, 0.123'456f
| Type | Size (in bytes) | Range | Example |
|---|---|---|---|
signed int |
4 | −2³¹ to 2³¹−1 | int i{-7}; |
unsigned int |
4 | 0 to 2³²−1 | unsigned int i{7}; |
signed short int |
2 | −2¹⁵ to 2¹⁵−1 | short i{-7}; |
unsigned short int |
2 | 0 to 2¹⁶−1 | unsigned short i{7}; |
signed long int |
4 or 8 | −2³¹ to 2³¹−1 (4 bytes) or −2⁶³ to 2⁶³−1 (8 bytes) | long i{-7}; |
unsigned long int |
4 or 8 | 0 to 2³²−1 (4 bytes) or 0 to 2⁶⁴−1 (8 bytes) | unsigned long i{7}; |
signed long long int |
8 | −2⁶³ to 2⁶³−1 | long long i{-7}; |
unsigned long long int |
8 | 0 to 2⁶⁴−1 | unsigned long long i{7}; |
float |
4 | ~±3.4×10⁻³⁸ to ±3.4×10³⁸ | float f{-7.5f}; |
double |
8 | ~±1.7×10⁻³⁰⁸ to ±1.7×10³⁰⁸ | double d{-7.5}; |
long double |
8, 12, or 16 | Platform-dependent (typically larger than double) |
long double ld{-7.5}; |
| Type | Size (in bytes) | Range | Usage Example |
|---|---|---|---|
char |
1 | Typically −128 to 127 or 0 to 255 (platform-dependent) | char c{'A'}; |
unsigned char |
1 | 0 to 255 | unsigned char c{65}; |
signed char |
1 | −128 to 127 | signed char c{-65}; |
char8_t |
1 | 0 to 255 | char8_t c{u8'A'}; |
char16_t |
2 | 0 to 2¹⁶−1 | char16_t c{u'A'}; |
char32_t |
4 | 0 to 2³²−1 | char32_t c{U'A'}; |
wchar_t |
2 or 4 | Platform-dependent (commonly 2 bytes on Windows, 4 bytes on Linux) | wchar_t wc{L'A'}; |
bool |
1 | true or false |
bool b{true}; |
<cstddef> provides std::byte to represent byte whereas signed and unsigned were used before (e.g. now it uses std::byte b { 42 };)INT_MAX used in C could be used but it's recommended to use std::numeric_limits in <limits>. Input
cout << "\ndouble:\n";
cout << format("Max double value: {}\n", numeric_limits<double>::max());
cout << format("Min double value: {}\n", numeric_limits<double>::min());
cout << format("Lowest double value: {}\n", numeric_limits<double>::lowest());
Output
double:
Max double value: 1.79769e+308
Min double value: 2.22507e-308
Lowest double value: -1.79769e+308
min() equals to lowest value in int but for floating point types, it gives smallest positive value that can be represented whereas lowest() gives the most negative value representable (i.e. lowest() = -max()){0} = {}: i.e. 0 here is optional and {} is called zero initializer.float MyFloat { 3.14f };
int i1 { (int)myFloat }; // NOT recommended
int i2 { int(myFloat) }; // not really used
int i3 { static_cast<int>(myFloat) }; // recommended
std::isinf() to check.std::isnan() to check.<cmath>numeric_limits (e.g. numeric_ limits<double>::infinity)