转义字符的迷惑

flying_music 2016-05-06 05:05:01
转义字符有些是为了控制输出格式,比如换行,分页

有些是为了区分界定符和内容,比如单引号、双引号

但‘\?’是干什么的啊?为什么要对问号转义啊?而且好像不写转义直接写问号也是可以的啊

为什么要多转义一个问号\?
...全文
115 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-05-06
  • 打赏
  • 举报
回复
稍不注意,竟然让1楼抢了先。
赵4老师 2016-05-06
  • 打赏
  • 举报
回复
Trigraphs The source character set of C source programs is contained within the 7-bit ASCII character set but is a superset of the ISO 646-1983 Invariant Code Set. Trigraph sequences allow C programs to be written using only the ISO (International Standards Organization) Invariant Code Set. Trigraphs are sequences of three characters (introduced by two consecutive question marks) that the compiler replaces with their corresponding punctuation characters. You can use trigraphs in C source files with a character set that does not contain convenient graphic representations for some punctuation characters. Table 1.1 shows the nine trigraph sequences. All occurrences in a source file of the punctuation characters in the first column are replaced with the corresponding character in the second column. Table 1.1 Trigraph Sequences Trigraph Punctuation Character ??= # ??( [ ??/ \ ??) ] ??’ ^ ??< { ??! | ??> } ??- ~ A trigraph is always treated as a single source character. The translation of trigraphs takes place in the firsttranslation phase, before the recognition of escape characters in string literals and character constants. Only the nine trigraphs shown in Table 1.1 are recognized. All other character sequences are left untranslated. The character escape sequence, \?, prevents the misinterpretation of trigraph-like character sequences. (For information about escape sequences, see Escape Sequences.) For example, if you attempt to print the string What??! with this printf statement printf( "What??!\n" ); the string printed is What| because ??! is a trigraph sequence that is replaced with the | character. Write the statement as follows to correctly print the string: printf( "What?\?!\n" ); In this printf statement, a backslash escape character in front of the second question mark prevents the misinterpretation of ??! as a trigraph.
小灸舞 2016-05-06
  • 打赏
  • 举报
回复
主要是来防止trigraphs

The question mark escape sequence \? is used to prevent trigraphs from being interpreted inside string literals: a string such as "??/" is compiled as "\", but if the second question mark is escaped, as in "?\?/", it becomes "??/"

参考:http://en.cppreference.com/w/cpp/language/escape

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧