在C和C ++(以及其他几种语言)中,字符和字符串常量中的水平制表符(ASCII代码9)以转义形式表示为'\t' \t。但是,我经常在字符串文字中键入未转义的制表符字符,例如在A	 B(betreen中有一个TAB A 和 B ),至少clang ++似乎并不麻烦 - 字符串似乎等效于A \tB。我更喜欢非转义的版本,因为长缩进的多行字符串在源代码中更好的可读性。
In C and C++ (and several other languages) horizontal tabulators (ASCII code 9) in character and string constants are denoted in escaped form as '\t' and "\t". However, I am regularly typing the unescaped tabulator character in string literals as for example in "A B" (there is a TAB in betreen A and B), and at least clang++ does not seem to bother - the string seems to be equivalent to "A\tB". I like the unescaped version better since long indented multi-line strings are better readable in the source code.
现在我问自己这是否在C和C ++或者只是我的编译器支持。如何可移植是字符和字符串常量中的非转义制表符?
Now I am asking myself whether this is generally legal in C and C++ or just supported by my compiler. How portable are unescaped tabulators in character and string constants?
令人惊讶的是,我找不到这个看似简单的问题的答案,无论是谷歌还是stackoverflow(我只是发现这个模糊的相关问题)。
Surprisingly I could not find an answer to this seemingly simple question, neither with Google nor on stackoverflow (I just found this vaguely related question).
推荐答案是的,您可以在字符串或字符文字中包含制表符字符,至少根据C ++ 11。允许的字符包括(我强调)
Yes, you can include a tab character in a string or character literal, at least according to C++11. The allowed characters include (with my emphasis):
源字符集的任何成员b双引号,反斜杠 \ 或换行字符
any member of the source character set except the double-quote ", backslash \, or new-line character
(从 C ++ 11 standard ,附件A.2)
,源字符集包括:
空格字符,表示水平标签,垂直标签,换页符和换行符的控制字符以及以下91个图形字符
the space character, the control characters representing horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters
(来自 C ++ 11 standard ,paragraph 2.3.1)
(from C++11 standard, paragraph 2.3.1)
更新:对于C99,答案也是肯定的,措辞是不同的,但基本上说同样的事情:
UPDATE: I've just noticed that you're asking about two different languages. For C99, the answer is also yes. The wording is different, but basically says the same thing:
在字符常量或字符串字面量中,执行字符集的成员应由源字符集或[...]的对应成员表示
In a character constant or string literal, members of the execution character set shall be represented by corresponding members of the source character set or [...]
其中源字符集和执行字符集都包含
where both the source and execution character sets include
标签,垂直标签和表单Feed。
control characters representing horizontal tab, vertical tab, and form feed.
更多推荐
是否必须在C和C ++中转义制表符字符?
发布评论