C++C++11C++14C++17C++20Learn C++

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

PrefixLiteralBaseExample
-no prefix-Decimal LiteralsBase 10 1024
0 (zero)Octa LiteralsBase 8048
0x or 0XHex LiteralsBase 160xFF
0b or 0BBinary LiteralsBase 20b01010111

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),

SuffixDecimal TypeOther 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 Llong int
unsigned long int
long long int
long int
unsigned long int
long long int
unsigned long long int
ll or LLlong long intlong 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 Uunsigned int
unsigned long int
unsigned long long int
unsigned int
unsigned long int
unsigned long long int
ul or ULunsigned long int
unsigned long long int
unsigned long int
unsigned long long int
ull or ULLunsigned long long intunsigned 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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?