转义的常规字符 [英] Escaped regular characters
问题描述
诸如"\ $"
之类的字符串是否非法?为什么或者为什么不?(Gcc和clang给出警告,但将其视为"$"
对待.) \
后面跟一个没有加反斜杠的字符的情况如何应该保留的保留转义序列?
Are strings such as "\$"
illegal? Why or why not? (Gcc and clang give a warning but treat it as if it was "$"
) How is \
followed by a character that, with the backslash prepended, doesn't form a reserved escape sequence supposed to behave?
推荐答案
在C11的n1570草案的(非规范性)注释中是明确的.6.4.4.4字符常量段落将转义序列定义为:
It is explicit in a (non normative) note of the draft n1570 for C11. The 6.4.4.4 Character constants paragraph defines escape sequences as:
转义序列:
escape-sequence:
- 简单转义序列
- 八进制转义序列
- 十六进制转义序列
- 通用字符名称
简单转义序列:
simple-escape-sequence: one of
- \'\"\?\
- \ a \ b \ f \ n \ r \ t \ v
八进制转义序列:
octal-escape-sequence:
- \八位数字
- \八进制数字八进制数字
- \八进制数字八进制数字八进制数字
十六进制转义序列:
hexadecimal-escape-sequence:
- \ x十六进制数字
- 十六进制转义序列十六进制数字
在 \
之后的另一个字符的所有其他序列未在此处定义,因此该行为未按当前标准指定(不是未定义)
All other sequences of another character following a \
are not defined here, so the behaviour is unspecified (not undefined) by current standard
注释说:
77)... 如果反斜杠后面还有其他字符,结果不是令牌,需要进行诊断.请参阅未来语言说明"(6.11.4).
77) ... If any other character follows a backslash, the result is not a token and a diagnostic is required. See ‘‘future language directions’’ (6.11.4).
6.11.4说:
6.11.4字符转义序列
6.11.4 Character escape sequences
小写字母作为转义序列保留用于将来的标准化.其他扩展名中可以使用字符.
Lowercase letters as escape sequences are reserved for future standardization. Other characters may be used in extensions.
通常,编译器会发出必需的警告,但只会忽略多余的 \
.它可以作为本地扩展名,因此与非小写字母完全一致,但由于它明确地是保留的功能
Commonly, compilers issue the required warning but just ignore the superfluous \
. It is fully conformant for non lowercase letters as it can be a local extension, but it could break in a future version of the C language for lower case letters because it is explicitely a reserved feature
这篇关于转义的常规字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!