From 2.13.2/3
The double quote
"
and the question mark?
, can be represented as themselves or by the escape sequences\"
and\?
[...].
Simply put, the following:
char x = '\?'; //or '\"'
char y = '?'; //or '"'
represent the same character. Why treat these two (especially ?
) differently than other characters?
Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark (").
Escapes are very useful for representing characters that are not apparent or are ambiguous. Numeric or named character references, as well as CSS escapes, can be used to represent characters in HTML style attribute. The style element HTML can not contain numeric or named character references.
In order to use a literal ^ at the start or a literal $ at the end of a regex, the character must be escaped. Some flavors only use ^ and $ as metacharacters when they are at the start or end of the regex respectively. In those flavors, no additional escaping is necessary. It's usually just best to escape them anyway.
Escaping a string means to reduce ambiguity in quotes (and other characters) used in that string. For instance, when you're defining a string, you typically surround it in either double quotes or single quotes: "Hello World."
\"
gives consistency between single-quoted character literals and double-quoted string literals (they're defined to use the same escape sequences, as a result \'
and \"
can be used in both). I'm slightly guessing, but I reckon the committee just figured it was too much bother to define different escape sequences in each, for no benefit and arguably a slight detriment.
\?
is for avoiding trigraphs: ??=
is a trigraph, ?\?=
isn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With