General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
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 | ||||||||||||||||
General | |||||||||||||||||||||
Literals | |||||||||||||||||||||
| |||||||||||||||||||||
Operators | |||||||||||||||||||||
Conversions | |||||||||||||||||||||
Allows values of integer type to be used in expressions directly.
Contents |
An integer literal has the form
decimal-literalinteger-suffix (optional) | (1) | ||||||||
octal-literalinteger-suffix (optional) | (2) | ||||||||
hex-literalinteger-suffix (optional) | (3) | ||||||||
binary-literalinteger-suffix (optional) | (4) | (since C++14) | |||||||
where
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
)0
) followed by zero or more octal digits (0
,1
,2
,3
,4
,5
,6
,7
)0x
or the character sequence0X
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
)0b
or the character sequence0B
followed by one or more binary digits (0
,1
)u
or the characterU
)l
or the characterL
)
| (since C++11) |
| (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 aprimary expression.
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:
unsignedlonglong l1= 18446744073709550592ull;// C++11unsignedlonglong l2=18'446'744'073'709'550'592llu;// C++14unsignedlonglong l3=1844'6744'0737'0955'0592uLL;// C++14unsignedlonglong l4=184467'440737'0'95505'92LLU;// C++14
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 whichinteger-suffix was used:
Suffix | Decimal bases | Binary, octal, or hexadecimal bases |
---|---|---|
(no suffix) |
|
|
u orU |
|
|
l orL |
|
|
bothl /L and u /U |
|
|
ll orLL |
|
|
bothll /LL and u /U |
|
|
z orZ |
|
|
bothz /Z and u /U |
|
|
If the value of the integer literalthat does not havesize-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.
Letters in the integer literals are case-insensitive:0xDeAdBeEfU
and0XdeadBEEFu
represent the same number(one exception is thelong-long-suffix, which is eitherll
orLL
, neverlL
orLl
)(since C++11).
There are no negative integer literals. Expressions such as-1 apply theunary 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 inlongint are allowed to have the typeunsignedlongint.
When used in a controlling expression of#if or#elif, all signed integer constants act as if they have typestd::intmax_t and all unsigned integer constants act as if they have typestd::uintmax_t. | (since C++11) |
Due tomaximal munch, hexadecimal integer literals ending ine
andE
, 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 macro | Value | Std | Feature |
---|---|---|---|
__cpp_binary_literals | 201304L | (C++14) | Binary literals |
__cpp_size_t_suffix | 202011L | (C++23) | Literal suffixes forstd::size_t and its signed version |
#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 literalstd::cout<<-9223372036854775808u<<'\n';// unary minus applied to unsigned// value subtracts it from 2^64, this gives 9223372036854775808std::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
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 2698 | C++23 | an integer literal withsize-suffix could have an extended integer type | ill-formed if too large |
user-defined literals(C++11) | literals with user-defined suffix[edit] |
C documentation forinteger constant |