"\$"
这样的字符串是非法的吗?为什么或者为什么不? (Gcc 和 clang 发出警告,但将其视为 "$"
)如何 \
后跟一个带有反斜杠的字符不形成一个保留的转义序列应该表现如何?
最佳答案
在 C11 的 n1570 草案的(非规范性)注释中有明确说明。 6.4.4.4 字符常量段落将转义序列定义为:
escape-sequence:
- simple-escape-sequence
- octal-escape-sequence
- hexadecimal-escape-sequence
- universal-character-name
simple-escape-sequence: one of
- \' \" \? \
- \a \b \f \n \r \t \v
octal-escape-sequence:
- \ octal-digit
- \ octal-digit octal-digit
- \ octal-digit octal-digit octal-digit
hexadecimal-escape-sequence:
- \x hexadecimal-digit
- hexadecimal-escape-sequence hexadecimal-digit
\
之后的所有其他字符序列未在此处定义,因此当前标准未指定(不是未定义)行为
一条注释说:
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 Character escape sequences
Lowercase letters as escape sequences are reserved for future standardization. Other characters may be used in extensions.
通常,编译器会发出所需的警告,但会忽略多余的 \
。它完全符合非小写字母,因为它可以是本地扩展,但它可能会在 C 语言的 future 版本中破坏小写字母,因为它显然是一个保留功能
关于c - 转义常规字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40863676/