Difference between revisions of "cpp/language/integer literal"
m (Capitalize the title) |
(P0330R8 Literal Suffix for (signed) size_t) |
||
Line 17: | Line 17: | ||
* {{spar|integer-suffix}}, if provided, may contain one or both of the following (if both are provided, they may appear in any order: | * {{spar|integer-suffix}}, if provided, may contain one or both of the following (if both are provided, they may appear in any order: | ||
:* {{spar|unsigned-suffix}} (the character {{tt|u}} or the character {{tt|U}}) | :* {{spar|unsigned-suffix}} (the character {{tt|u}} or the character {{tt|U}}) | ||
− | :* {{spar|long-suffix}} (the character {{tt|l}} or the character {{tt|L}}){{ | + | :* {{spar|long-suffix}} (the character {{tt|l}} or the character {{tt|L}}) |
− | {{ | + | {{rrev|since=c++11| |
− | {{ | + | :* {{spar|long-long-suffix}} (the character sequence {{tt|ll}} or the character sequence {{tt|LL}}) |
− | + | }} | |
+ | {{rrev|since=c++23| | ||
+ | :* {{spar|size-suffix}} (the character sequence {{tt|z}} or the character sequence {{tt|Z}}) | ||
+ | }} | ||
+ | {{rrev | since = c++14 | | ||
+ | Optional single quotes({{tt|'}}) may be inserted between the digits as a separator. They are ignored by the compiler. | ||
+ | }} | ||
+ | |||
===Explanation=== | ===Explanation=== | ||
@1@ Decimal integer literal (base 10, the first digit is the most significant) | @1@ Decimal integer literal (base 10, the first digit is the most significant) | ||
Line 97: | Line 104: | ||
| {{c|unsigned long long int}}{{mark since c++11}} | | {{c|unsigned long long int}}{{mark since c++11}} | ||
| {{c|unsigned long long int}}{{mark since c++11}} | | {{c|unsigned long long int}}{{mark since c++11}} | ||
+ | |- | ||
+ | | {{tt|z}} or {{tt|Z}} | ||
+ | | the signed integer type corresponding to {{lc|std::size_t}}{{mark since c++23}} | ||
+ | | the signed integer type corresponding to {{lc|std::size_t}}{{mark since c++23}} | ||
+ | {{lc|std::size_t}}{{mark since c++23}} | ||
+ | |- | ||
+ | | both {{tt|z}}/{{tt|Z}} and {{tt|u}}/{{tt|U}} | ||
+ | | {{lc|std::size_t}}{{mark since c++23}} | ||
+ | | {{lc|std::size_t}}{{mark since c++23}} | ||
|} | |} | ||
Line 160: | Line 176: | ||
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Revision as of 23:11, 14 November 2020
Allows values of integer type to be used in expressions directly.
Contents |
Syntax
An integer literal is a primary expression of 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 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
) - binary-literal is the character sequence
0b
or the character sequence0B
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 characterU
) - long-suffix (the character
l
or the characterL
)
- unsigned-suffix (the character
|
(since C++11) |
|
(since C++23) |
Optional single quotes( |
(since C++14) |
Explanation
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
The following variables are also initialized to the same value:
unsigned long long l1 = 18446744073709550592ull; // C++11 unsigned long long l2 = 18'446'744'073'709'550'592llu; // C++14 unsigned long long l3 = 1844'6744'0737'0955'0592uLL; // C++14 unsigned long long l4 = 184467'440737'0'95505'92LLU; // C++14
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.
Types allowed for integer literals | ||
---|---|---|
suffix | decimal bases | binary, octal, or hexadecimal bases |
no suffix | int
long int |
int
unsigned int |
u or U
|
unsigned int
unsigned long int |
unsigned int
unsigned long int |
l or L
|
long int
unsigned long int(until C++11) |
long int
unsigned long int |
both l /L and u /U
|
unsigned long int
unsigned long long int(since C++11) |
unsigned long int(since C++11)
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
|
the signed integer type corresponding to std::size_t(since C++23) | the signed integer type corresponding to std::size_t(since C++23)
std::size_t(since C++23) |
both z /Z and u /U
|
std::size_t(since C++23) | std::size_t(since C++23) |
If the value of the integer literal is too big to fit in any of the types allowed by suffix/base combination and the compiler supports extended integer types (such as __int128) the literal may be given the extended integer type -- otherwise the program is ill-formed.
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
)
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.
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; // error auto y = 0xa+2.0; // OK auto z = 0xE +2.0; // OK auto q = (0xE)+2.0; // OK
Otherwise, a single invalid preprocessing number token is formed, which causes further analysis to fail.
Example
#include <iostream> 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 }
Output:
123 83 291 2 12345678901234567890 12345678901234567890 9223372036854775808 -9223372036854775808
See also
user-defined literals(C++11) | literals with user-defined suffix |
C documentation for integer constant
|