Learn The Useful Integer Literals In Modern C++

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++.

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:

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++.

The C++23 feature can be tested with this __cpp_size_t_suffix if definition as we show below.

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++.

If you are looking more details about literals, there are many documents in https://www.open-std.org/

Learn The Useful Integer Literals In Modern C++ C++ Builder logo

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.