Why is “\?” an escape sequence in C/C++?

Why is \? one of the escape sequence characters?

Because it is special. The answer leads to Trigraph, and the C/C++ preprocessor replaces the following three-character sequences with the corresponding single character. (C11 § and C++11 §2.3)

Trigraph:       ??(  ??)  ??<  ??>  ??=  ??/  ??'  ??!  ??-
Replacement:      [    ]    {    }    #    \    ^    |    ~

A trigraph is nearly useless now, and it is mainly used for obfuscation purposes. Some examples can be seen in IOCCC.

GCC doesn’t support trigraph by default and will warn you if there’s a trigraph in the code, unless the option -trigraphs3 is enabled. Under the -trigraphs option, the second \? is useful in the following example:


Output would be | if ? is not escaped.

For more information on trigraphs, see Cryptic line “??!??!” in legacy code

Why does non-escaping ? work fine. There’s not even a warning.

Because ?(and double quote ") can be represented by themselves by the standard:

C11 § Character constants Section 4

The double-quote " and question-mark ? are representable either by themselves or by the escape sequences \" and \?, respectively, but the single-quote ' and the backslash \ shall be represented, respectively, by the escape sequences \' and \\.

Similar in C++:

C++11 §2.13.2 Character literals Section 3

Certain nongraphic characters, the single quote , the double quote ", the question mark ?, and the backslash \, can be represented according to Table 6. The double quote " and the question mark ?, can be represented as themselves or by the escape sequences \" and \? respectively, but the single quote and the backslash \ shall be represented by the escape sequences \’ and \\ respectively. If the character following a backslash is not one of those specified, the behavior is undefined. An escape sequence specifies a single character.

Leave a Comment