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

问题描述

有需要在C语言中可以逃脱四个特殊的非字母字符/ C ++:单引号 \\ ,双引号 \\ ,反斜线 \\\\ 和问号 \\ 。这显然是因为他们具有特殊的含义。 字符的字符串, \\ 的转义序列,但为什么是其中之一吗?

我在一本教科书今天读来转义序列的表,我意识到,我已经的从不逃跑之前,从来没有遇到问题有了它,只是要确定,我测试了它在海湾合作委员会:

 的#include<&stdio.h中GT;
INT主要(无效)
{
    的printf(问号逃到\\ \\ n?);
    返回0;
}

和C ++版本:

 的#include<&iostream的GT;
INT主要(无效)
{
    性病::法院LT&;< 问号?逃走\\? <<的std :: ENDL;
    返回0;
}

这两个程序的输出:问号?逃走?

所以,我有两个问题:


  1. 为什么 \\ 转义序列的人物之一?

  2. 为什么不逃跑工作正常,甚至有没有一个警告。


我要问这个问题之前,我发现自己的答案,因为我没有发现SO重复,我决定将它张贴在Q&安培; A风格

更有趣的事实是,逃脱 \\ 可以在其他语言中使用的相同还有,我在Lua / Ruby的测试,这也是事实,即使我没有找到这个文件。


解决方案

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 is nearly useless now, mainly used for obfuscated purpose, some examples can be seen in IOCCC.

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


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

Similar in C++:

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

08-22 21:52
查看更多