Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the rationale in allowing `?` to be escaped?

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?

like image 705
Luchian Grigore Avatar asked Jun 28 '12 09:06

Luchian Grigore


People also ask

What is the purpose of escape sequence?

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 (").

What is the purpose of the escape character in a HTML code?

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.

Does need to be escaped regex?

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.

What does it mean to escape a string?

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."


1 Answers

\" 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.

like image 123
Steve Jessop Avatar answered Oct 14 '22 07:10

Steve Jessop