Skip to content

Commit df24e5e

Browse files
author
Colin Robertson
committed
Clarify standard octal and hex behavior
1 parent dc8f99f commit df24e5e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

docs/cpp/string-and-character-literals-cpp.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ There are three kinds of escape sequences: simple, octal, and hexadecimal. Escap
109109
| alert (bell) | \\a |
110110
| hexadecimal | \\xhhh |
111111

112+
An octal escape sequence is a backslash followed by a sequence of one to three octal digits. An octal escape sequence terminates at the first character that's not an octal digit, if encountered sooner than the third digit.
113+
114+
A hexadecimal escape sequence is a backslash followed by the character `x`, followed by a sequence of hexadecimal digits. An escape sequence that contains no hexadecimal digits causes compiler error C2153: "hex literals must have at least one hex digit". Leading zeroes are ignored.
115+
112116
This sample code shows some examples of escaped characters using ordinary character literals. The same escape sequence syntax is valid for the other character literal types.
113117

114118
```cpp
@@ -143,7 +147,7 @@ wchar_t w0 = 'abcd'; // C4305, C4309, truncates to '\x6364'
143147
int i0 = 'abcd'; // 0x61626364
144148
```
145149

146-
An octal escape sequence is a backslash followed by a sequence of up to 3 octal digits. The behavior of an octal escape sequence that appears to contain more than three digits is treated as a 3-digit octal sequence, followed by the subsequent digits as characters in a multicharacter literal, which can give surprising results. For example:
150+
An octal escape sequence that appears to contain more than three digits is treated as a 3-digit octal sequence, followed by the subsequent digits as characters in a multicharacter literal, which can give surprising results. For example:
147151

148152
```cpp
149153
char c1 = '\100'; // '@'
@@ -158,7 +162,7 @@ char c4 = '\089'; // C4305, C4309, truncates to '9'
158162
char c5 = '\qrs'; // C4129, C4305, C4309, truncates to 's'
159163
```
160164

161-
A hexadecimal escape sequence is a backslash followed by the character `x`, followed by a sequence of hexadecimal digits. An escape sequence that contains no hexadecimal digits causes compiler error C2153: "hex literals must have at least one hex digit". Leading zeroes are ignored. An escape sequence that appears to have hexadecimal and non-hexadecimal characters is evaluated as a multicharacter literal that contains a hexadecimal escape sequence up to the last hexadecimal character, followed by the non-hexadecimal characters. In an ordinary or u8-prefixed character literal, the highest hexadecimal value is 0xFF. In an L-prefixed or u-prefixed wide character literal, the highest hexadecimal value is 0xFFFF. In a U-prefixed wide character literal, the highest hexadecimal value is 0xFFFFFFFF.
165+
An escape sequence that appears to have hexadecimal and non-hexadecimal characters is evaluated as a multicharacter literal that contains a hexadecimal escape sequence up to the last hexadecimal character, followed by the non-hexadecimal characters. In an ordinary or u8-prefixed character literal, the highest hexadecimal value is 0xFF. In an L-prefixed or u-prefixed wide character literal, the highest hexadecimal value is 0xFFFF. In a U-prefixed wide character literal, the highest hexadecimal value is 0xFFFFFFFF.
162166

163167
```cpp
164168
char c6 = '\x0050'; // 'P'

0 commit comments

Comments
 (0)