Integer literal - cppreference.com (2024)

C++ language

General topics
Flow control
Conditional execution statements
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous

Expressions

General
Literals
Operators
Conversions

Allows values of integer type to be used in expressions directly.

Contents

  • 1 Syntax
  • 2 Explanation
  • 3 The type of the literal
  • 4 Notes
  • 5 Example
  • 6 Defect reports
  • 7 References
  • 8 See also

[edit] Syntax

An integer literal has the form

decimal-literal integer-suffix (optional) (1)
octal-literal integer-suffix (optional) (2)
hex-literal integer-suffix (optional) (3)
binary-literal integer-suffix (optional) (4) (since C++14)

where

  • decimal-literal is a non-zero decimal digit (1, 2, 3, 4, 5, 6, 7, 8, 9), followed by zero or more decimal digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  • octal-literal is the digit zero (0) followed by zero or more octal digits (0, 1, 2, 3, 4, 5, 6, 7)
  • hex-literal is the character sequence 0x or the character sequence 0X followed by one or more hexadecimal digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, A, b, B, c, C, d, D, e, E, f, F)
  • binary-literal is the character sequence 0b or the character sequence 0B followed by one or more binary digits (0, 1)
  • integer-suffix, if provided, may contain one or both of the following (if both are provided, they may appear in any order:
  • unsigned-suffix (the character u or the character U)
  • one of
  • long-suffix (the character l or the character L)
  • long-long-suffix (the character sequence ll or the character sequence LL)
(since C++11)
  • size-suffix (the character z or the character Z)
(since C++23)

Optional single quotes (') may be inserted between the digits as a separator; they are ignored when determining the value of the literal.

(since C++14)

An integer literal (as any literal) is a primary expression.

[edit] Explanation

1) Decimal integer literal (base 10).

2) Octal integer literal (base 8).

3) Hexadecimal integer literal (base 16, the letters 'a' through 'f' represent values (decimal) 10 through 15).

4) Binary integer literal (base 2).

The first digit of an integer literal is the most significant.

Example. The following variables are initialized to the same value:

int d = 42;int o = 052;int x = 0x2a;int X = 0X2A;int b = 0b101010; // C++14

Example. The following variables are also initialized to the same value:

unsigned long long l1 = 18446744073709550592ull; // C++11unsigned long long l2 = 18'446'744'073'709'550'592llu; // C++14unsigned long long l3 = 1844'6744'0737'0955'0592uLL; // C++14unsigned long long l4 = 184467'440737'0'95505'92LLU; // C++14

[edit] The type of the literal

The type of the integer literal is the first type in which the value can fit, from the list of types which depends on which numeric base and which integer-suffix was used:

SuffixDecimal basesBinary, octal, or hexadecimal bases
(no suffix)
  • int
  • long int
  • long long int (since C++11)
  • int
  • unsigned int
  • long int
  • unsigned long int
  • long long int (since C++11)
  • unsigned long long int (since C++11)
u or U
  • unsigned int
  • unsigned long int
  • unsigned long long int (since C++11)
  • unsigned int
  • unsigned long int
  • unsigned long long int (since C++11)
l or L
  • long int
  • unsigned long int (until C++11)
  • long long int (since C++11)
  • long int
  • unsigned long int
  • long long int (since C++11)
  • unsigned long long int (since C++11)
both l/L
and u/U
  • unsigned long int
  • unsigned long long int (since C++11)
  • unsigned long int
  • unsigned long long int (since C++11)
ll or LL
  • long long int (since C++11)
  • long long int (since C++11)
  • unsigned long long int (since C++11)
both ll/LL
and u/U
  • unsigned long long int (since C++11)
  • unsigned long long int (since C++11)
z or Z
both z/Z
and u/U

If the value of the integer literal that does not have size-suffix(since C++23) is too big to fit in any of the types allowed by suffix/base combination and the compiler supports an extended integer type (such as __int128) which can represent the value of the literal, the literal may be given that extended integer type — otherwise the program is ill-formed.

[edit] Notes

Letters in the integer literals are case-insensitive: 0xDeAdBeEfU and 0XdeadBEEFu represent the same number (one exception is the long-long-suffix, which is either ll or LL, never lL or Ll)(since C++11).

There are no negative integer literals. Expressions such as -1 apply the unary minus operator to the value represented by the literal, which may involve implicit type conversions.

In C prior to C99 (but not in C++), unsuffixed decimal values that do not fit in long int are allowed to have the type unsigned long int.

When used in a controlling expression of #if or #elif, all signed integer constants act as if they have type std::intmax_t and all unsigned integer constants act as if they have type std::uintmax_t.

(since C++11)

Due to maximal munch, hexadecimal integer literals ending in e and E, when followed by the operators + or -, must be separated from the operator with whitespace or parentheses in the source:

auto x = 0xE+2.0; // errorauto y = 0xa+2.0; // OKauto z = 0xE +2.0; // OKauto q = (0xE)+2.0; // OK

Otherwise, a single invalid preprocessing number token is formed, which causes further analysis to fail.

Feature-test macroValueStdFeature
__cpp_binary_literals201304L(C++14)Binary literals
__cpp_size_t_suffix202011L(C++23)Literal suffixes for std::size_t and its signed version

[edit] Example

Run this code

#include <cstddef>#include <iostream>#include <type_traits>int main(){ std::cout << 123 << '\n' << 0123 << '\n' << 0x123 << '\n' << 0b10 << '\n' << 12345678901234567890ull << '\n' << 12345678901234567890u << '\n'; // the type is unsigned long long // even without a long long suffix// std::cout << -9223372036854775808 << '\n'; // error: the value // 9223372036854775808 cannot fit in signed long long, which is the // biggest type allowed for unsuffixed decimal integer literal std::cout << -9223372036854775808u << '\n'; // unary minus applied to unsigned // value subtracts it from 2^64, this gives 9223372036854775808 std::cout << -9223372036854775807 - 1 << '\n'; // correct way to calculate // the value -9223372036854775808#if __cpp_size_t_suffix >= 202011L // C++23 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}

Output:

12383291212345678901234567890123456789012345678909223372036854775808-9223372036854775808

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
CWG 2698C++23an integer literal with size-suffix could have an extended integer typeill-formed if too large

[edit] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 5.13.2 Integer literals [lex.icon]
  • C++20 standard (ISO/IEC 14882:2020):
  • 5.13.2 Integer literals [lex.icon]
  • C++17 standard (ISO/IEC 14882:2017):
  • 5.13.2 Integer literals [lex.icon]
  • C++14 standard (ISO/IEC 14882:2014):
  • 2.14.2 Integer literals [lex.icon]
  • C++11 standard (ISO/IEC 14882:2011):
  • 2.14.2 Integer literals [lex.icon]
  • C++98 standard (ISO/IEC 14882:1998):
  • 2.13.1 Integer literals [lex.icon]

[edit] See also

user-defined literals(C++11) literals with user-defined suffix[edit]

C documentation for integer constant

Integer literal - cppreference.com (2024)
Top Articles
How can you write efficient test cases from software requirements?
FAQs on Measures of Central Tendency - Mean, Mode and Median
Melson Funeral Services Obituaries
Washu Parking
Instructional Resources
Baseball-Reference Com
Tiger Island Hunting Club
Craigslist Jobs Phoenix
Bahsid Mclean Uncensored Photo
State HOF Adds 25 More Players
Espn Horse Racing Results
Log in or sign up to view
Ubg98.Github.io Unblocked
Aps Day Spa Evesham
Tyrone Unblocked Games Bitlife
2013 Ford Fusion Serpentine Belt Diagram
Amazing Lash Studio Casa Linda
Air Quality Index Endicott Ny
Doki The Banker
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
A Man Called Otto Showtimes Near Cinemark University Mall
Danielle Ranslow Obituary
Parkeren Emmen | Reserveren vanaf €9,25 per dag | Q-Park
Amelia Chase Bank Murder
Albert Einstein Sdn 2023
JVID Rina sauce set1
Vera Bradley Factory Outlet Sunbury Products
Effingham Daily News Police Report
Gt7 Roadster Shop Rampage Engine Swap
Salemhex ticket show3
Craigslist Texas Killeen
"Pure Onyx" by xxoom from Patreon | Kemono
Workboy Kennel
Leland Nc Craigslist
Domino's Delivery Pizza
Academic important dates - University of Victoria
ENDOCRINOLOGY-PSR in Lewes, DE for Beebe Healthcare
Second Chance Apartments, 2nd Chance Apartments Locators for Bad Credit
Bill Manser Net Worth
ACTUALIZACIÓN #8.1.0 DE BATTLEFIELD 2042
Arnesons Webcam
Booknet.com Contract Marriage 2
Fairbanks Auto Repair - University Chevron
Pike County Buy Sale And Trade
Hanco*ck County Ms Busted Newspaper
Haunted Mansion (2023) | Rotten Tomatoes
Tyco Forums
How to Connect Jabra Earbuds to an iPhone | Decortweaks
6463896344
Oak Hill, Blue Owl Lead Record Finastra Private Credit Loan
Grandma's Portuguese Sweet Bread Recipe Made from Scratch
32 Easy Recipes That Start with Frozen Berries
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 5946

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.