11 KiB
[lex.icon]
5 Lexical conventions [lex]
5.13 Literals [lex.literal]
5.13.2 Integer literals [lex.icon]
integer-literal:
binary-literal integer-suffixopt
octal-literal integer-suffixopt
decimal-literal integer-suffixopt
hexadecimal-literal integer-suffixopt
binary-literal:
0b binary-digit
0B binary-digit
binary-literal 'opt binary-digit
octal-literal:
0
octal-literal 'opt octal-digit
decimal-literal:
nonzero-digit
decimal-literal 'opt digit
hexadecimal-literal:
hexadecimal-prefix hexadecimal-digit-sequence
binary-digit: one of
0 1
octal-digit: one of
0 1 2 3 4 5 6 7
nonzero-digit: one of
1 2 3 4 5 6 7 8 9
hexadecimal-prefix: one of
0x 0X
hexadecimal-digit-sequence:
hexadecimal-digit
hexadecimal-digit-sequence 'opt hexadecimal-digit
hexadecimal-digit: one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
integer-suffix:
unsigned-suffix long-suffixopt
unsigned-suffix long-long-suffixopt
unsigned-suffix size-suffixopt
long-suffix unsigned-suffixopt
long-long-suffix unsigned-suffixopt
size-suffix unsigned-suffixopt
unsigned-suffix: one of
u U
long-suffix: one of
l L
long-long-suffix: one of
ll LL
size-suffix: one of
z Z
In an integer-literal, the sequence ofbinary-digits,octal-digits,digits, orhexadecimal-digits is interpreted as a base N integer as shown in Table 7; the lexically first digit of the sequence of digits is the most significant.
[Note 1:
The prefix and any optional separating single quotes are ignored when determining the value.
â end note]
Table 7 — Base of integer-literals [tab:lex.icon.base]
| ð Kind of integer-literal |
base N |
|---|---|
| ð binary-literal |
2 |
| ð octal-literal |
8 |
| ð decimal-literal |
10 |
| ð hexadecimal-literal |
16 |
The hexadecimal-digitsa through f and A through F have decimal values ten through fifteen.
[Example 1:
The number twelve can be written 12, 014,0XC, or 0b1100.
The integer-literals 1048576,1'048'576, 0X100000, 0x10'0000, and0'004'000'000 all have the same value.
â end example]
The type of an integer-literal is the first type in the list in Table 8 corresponding to its optional integer-suffix in which its value can be represented.
Table 8 — Types of integer-literals [tab:lex.icon.type]
| ð integer-suffix |
decimal-literal | integer-literal other than decimal-literal |
|---|---|---|
| ð none |
int | int |
| ð | long int | unsigned int |
| ð | long long int | long int |
| ð | unsigned long int | |
| ð | long long int | |
| ð | unsigned long long int | |
| ð u or U |
unsigned int | unsigned int |
| ð | unsigned long int | unsigned long int |
| ð | unsigned long long int | unsigned long long int |
| ð l or L |
long int | long int |
| ð | long long int | unsigned long int |
| ð | long long int | |
| ð | unsigned long long int | |
| ð Both u or U |
unsigned long int | unsigned long int |
| ð and l or L |
unsigned long long int | unsigned long long int |
| ð ll or LL |
long long int | long long int |
| ð | unsigned long long int | |
| ð Both u or U |
unsigned long long int | unsigned long long int |
| ð and ll or LL |
||
| ð z or Z |
the signed integer type corresponding | the signed integer type |
| ð | to std::size_t ([support.types.layout]) | corresponding to std::size_t |
| ð | std::size_t | |
| ð Both u or U |
std::size_t | std::size_t |
| ð and z or Z |
Except for integer-literals containing a size-suffix, if the value of an integer-literal cannot be represented by any type in its list and an extended integer type ([basic.fundamental]) can represent its value, it may have that extended integer type.
If all of the types in the list for the integer-literal are signed, the extended integer type is signed.
If all of the types in the list for the integer-literal are unsigned, the extended integer type is unsigned.
If the list contains both signed and unsigned types, the extended integer type may be signed or unsigned.
If an integer-literal cannot be represented by any of the allowed types, the program is ill-formed.
[Note 2:
An integer-literal with a z or Z suffix is ill-formed if it cannot be represented by std::size_t.
â end note]