为什么是“\?” C / C ++中的转义序列? [英] Why is "\?" an escape sequence in C/C++?

查看:470
本文介绍了为什么是“\?” C / C ++中的转义序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有四个特殊的非字母字符需要在C / C ++中转义:单引号 \',双引号 \,反斜杠 \\ 和问号 \?这显然是因为它们有特殊的含义。'单个 char 用于字符串文字, \ 用于转义序列,但为什么其中之一?

There are four special non-alphabet characters that need to be escaped in C/C++: the single quote \', the double quote \", the backslash \\ and the question mark \?. It's apparently because they have special meanings. ' for single char, " for string literals, \ for escape sequences, but why is ? one of them?

我今天阅读了一本教科书中的转义序列表,我意识到我已经永远不会转义之前,从来没有遇到过问题,只是为了确保,我在gcc下测试:

I read the table of escape sequences in a textbook today and I realized that I've never escape ? before and never encountered a problem with it , just to be sure, I tested it under gcc:

#include <stdio.h>
int main(void)
{
    printf("question mark ? and escaped \?\n");
    return 0;
}

和C ++版本:

#include <iostream>
int main(void)
{
    std::cout << "question mark ? and escaped \?" << std::endl;
    return 0;
}

两个程序输出:问号?并逃脱?

所以我有两个问题:


  1. 为什么不转义?/ code? 工作正常,甚至没有警告。

  1. Why is \? one of the escape sequence characters ?
  2. Why non-escaping ? works fine, there's not even a warning.






在我要提出这个问题之前,我自己找到了答案,因为我没有在SO中找到一个重复的文件,所以我决定以Q& A的形式发布。


Before I'm about to ask this question, I found the answer myself, since I didn't find a duplicate in SO, I decided to post it in Q&A style.

更有意思的是,转义的 \?可以与其他一些一样使用语言以及我在Lua / Ruby中测试,即使我没有找到这个记录也是如此。

The more interesting fact is that the escaped \? can be used the same as ? in some other languages as well, I tested in Lua/Ruby, it's also true even though I didn't find this documented.

推荐答案


为什么 \?其中一个转义序列字符?

Why is \? one of the escape sequence characters ?

因为它是特别的,答案导致 Trigra ph ,C / C ++预处理器将以下三个字符的序列替换为相应的单个字符。 (C11§5.2.1.1和C ++ 11§2.3)

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

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

现在,Trigraph几乎没用,主要用于混淆目的,一些例子可以在 IOCCC

Trigraph is nearly useless now, mainly used for obfuscated purpose, some examples can be seen in IOCCC.

gcc默认情况下不支持三角形,如果代码中有三角形,则会发出警告,除非选项 -trigraphs 3 已启用。在 -trigraphs 选项下,第二个 \?在以下示例中很有用:

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

printf("\?\?!\n");  

输出将为 | 如果不会转义。

Output would be | if ? is not escaped.

有关三位图的更多信息,请参阅加密行??!??!在旧版代码中

For more information on trigraph, see Cryptic line "??!??!" in legacy code


为什么不转义工作正常,甚至没有警告。

Why non-escaping ? works fine, there's not even a warning.

因为? code>(和双引号)可以由标准本身表示:

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


C11§6.4.4.4字符常量第4节



双引号和问号可以由自己或转义序列 \ \?,但单引号'和反斜杠 \ 分别由转义序列 \' \\ 表示。

C11 §6.4.4.4 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 \\.

与C ++类似:


C ++ 11§2.13.2字符文字第3节



某些非字符字符,单个te ',双引号,问号,并且反斜杠 \ 可以根据表6表示。双引号和问号可以表示为自己或转义序列 \ \ ?,但单引号'和反斜杠 \ 应由转义序列分别为 \' \\ 。如果反斜杠后面的字符不是指定的那个,则行为是未定义的。转义序列指定单个字符。

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.

这篇关于为什么是“\?” C / C ++中的转义序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆