A programming language consists of a lot of expressions in our source code. One of elements of expressions in C++ are literals. A more specific type of literal is Integer Literals. Integer literals are primary expressions that allow values of integer types (i.e., decimal, octal, hexadecimal) to be used in expressions directly. They are further improved by the boolean
literals (binary digits) in C++14. In this post, we will explain and list the types of integer literals available to us in modern C++.
Table of Contents
What is integer literal in modern C++?
Integer Literals in C++ are primary expressions that are used to indicate the base or the type of an integer value. They allow values of integer types to be used in expressions directly. They are further improved by the addition of boolean
literals in C++ 14.
We can categorize integer literals into two groups: Prefixes and Suffixes.
What is the prefix integer literal in modern C++?
Prefix Integer Literals are used to indicate the numeric base of the literal. For example, the prefix 0x
indicates a hexadecimal base (i.e. value 0x10
indicates the value of 16).
Here are the prefixes for integer literals (integer-prefix).
Prefix | Literal | Base | Example |
-no prefix- | Decimal Literals | Base 10 | 1024 |
0 (zero) | Octa Literals | Base 8 | 048 |
0x or 0X | Hex Literals | Base 16 | 0xFF |
0b or 0B | Binary Literals | Base 2 | 0b01010111 |
Here are simple examples to prefix integer literals:
1 2 3 4 5 6 |
int d = 1024; // decimal int o = 076; // octal int x = 0xFF22CC; // hexadecimal int b = 0b10101111; // binary (C++14) |
What is the suffix integer literal in modern C++?
Suffix Integer Literals are used to indicate the type. For example, the suffix LL
indicates the value is for the long long integer
(i.e. value 98765432109876LL indicates it is a long long integer
).
When we use suffix, that means this type of the integer literal is the first type in which the value can fit, and there are other decimal types from the list of types that depends on which numeric base and suffix.
Here are the suffixes for integer literals (integer-suffix),
Suffix | Decimal Type | Other Decimal Types |
-no suffix- | int long int long long int | int unsigned int long int unsigned long int long long int unsigned long long int |
l or L | long int unsigned long int long long int | long int unsigned long int long long int unsigned long long int |
ll or LL | long long int | long long int unsigned long long int |
z or Z (C++23) | the signed version of std::size_t | the signed version of std::size_t std::size_t |
u or U | unsigned int unsigned long int unsigned long long int | unsigned int unsigned long int unsigned long long int |
ul or UL | unsigned long int unsigned long long int | unsigned long int unsigned long long int |
ull or ULL | unsigned long long int | unsigned long long int |
uz or UZ (C++23) | std::size_t | std::size_t |
Here are simple examples of how to suffix integer literals in C++.
1 2 3 4 5 6 7 8 9 |
int i1 = 2048; long i2 = 2048L; // or l long long i3 = 2048LL; // or ll unsigned int u1 = 2048u; // or U unsigned long int u2 = 2048ul; // or UL unsigned long long int u3 = 2048ull; // or ULL |
The C++23 feature can be tested with this __cpp_size_t_suffix
if definition as we show below.
1 2 3 4 5 6 7 8 |
#if __cpp_size_t_suffix >= 202011L // (C++23 feature) auto size1 = 0z; // or Z auto size2 = 40uz; // or UZ static_assert( std::is_same_v<decltype(0uz), std::size_t> ); static_assert( std::is_same_v<decltype(0z), std::make_signed_t< std::size_t >> ); #endif |
Is there a full example about integer literals in modern C++?
Here is a full example of how to use integer literals that uses prefixes and suffixes in modern C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> void suffix_examples() { int d = 1024; // decimal int o = 076; // octal int x = 0xFF22CC; // hexadecimal int b = 0b10101111; // binary (C++14) } void prefix_examples() { int i1 = 2048; long i2 = 2048L; // or l long long i3 = 2048LL; // or ll unsigned int u1 = 2048u; // or U unsigned long int u2 = 2048ul; // or UL unsigned long long int u3 = 2048ull; // or ULL #if __cpp_size_t_suffix >= 202011L // (C++23 feature) auto size1 = 0z; // or Z auto size2 = 40uz; // or UZ static_assert( std::is_same_v<decltype(0uz), std::size_t> ); static_assert( std::is_same_v<decltype(0z), std::make_signed_t< std::size_t >> ); #endif } int main() { std::cout << "Testing Integer Literals\n"; suffix_examples(); prefix_examples(); system("pause"); return 0; } |
If you are looking more details about literals, there are many documents in https://www.open-std.org/
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.