C语言结构
为什么这有效
printf("Hello"
"World");
而
printf("Hello
""World");
无效? ANSI C 连接相邻的字符串,这没问题……但这是另一回事。 这和C语言解析器有什么关系吗? 谢谢
Why does this work
printf("Hello"
"World");
Whereas
printf("Hello
""World");
does not?
ANSI C concatenates adjacent Strings, that's ok... but it's a different thing.
Does this have something to do with the C language parser or something?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串必须在行尾之前终止。这是一件好事。否则,忘记的右引号可能会阻止后续代码行的执行。
这可能会花费大量时间进行调试。如今,语法着色可以提供线索,但早年有单色显示。
The string must be terminated before the end of the line. This is a good thing. Otherwise, a forgotten close-quote could prevent subsequent lines of code from executing.
This could cost significant time to debug. These days syntax coloring would provide a clue, but in the early years there were monochrome displays.
您无法在字符串文字中创建新行。这是我的 C 设计师做出的选择。在我看来,这是一个很好的功能。
但是,您可以这样做:
这会产生相同的结果。
You can't make a new line in a string literal. This was a choice made my the designers of C. IMO it's a good feature though.
You can however do this:
Which gives the same results.
C 语言是根据标记来定义的,其中一个标记是字符串文字(标准语言:s-char-sequence)。 s-char-sequences 以未转义的双引号开头和结尾,并且不得包含未转义的换行符。
相关标准 (C99) 引用:
然而,转义换行符在称为行拼接的早期翻译阶段被删除,因此编译器永远无法解释它们。以下是相关标准(C99)引用:
翻译语法规则的优先级由以下阶段指定。
空白字符(包括注释)。源文件不得以
部分预处理标记或部分注释中。每个评论都被替换为
一个空格字符。保留换行符。是否每个非空
保留除换行符之外的一系列空白字符或将其替换为一个空格字符是实现定义的。
_Pragma 一元运算符表达式被执行。如果一个字符序列
匹配由 token 生成的通用字符名称的语法
连接(6.10.3.3),行为未定义。 #include 预处理
指令导致指定的头文件或源文件从第 1 阶段开始处理
递归地完成第 4 阶段。然后删除所有预处理指令。
预处理token被转换成token。生成的令牌是
作为一个翻译单元进行句法和语义分析和翻译。
The C language is defined in terms of tokens and one of the tokens is a string literal (in standardese: an s-char-sequence). s-char-sequences start and end with unescaped double quotes and must not contain an unescaped newline.
Relevant standard (C99) quote:
Escaped newlines, however, are removed in an early translation phase called line splicing, so the compiler never gets to interpret them. Here's the relevant standard (C99) quote:
The precedence among the syntax rules of translation is specified by the following phases.
white-space characters (including comments). A source file shall not end in a
partial preprocessing token or in a partial comment. Each comment is replaced by
one space character. New-line characters are retained. Whether each nonempty
sequence of white-space characters other than new-line is retained or replaced by one space character is implementation-defined.
_Pragma unary operator expressions are executed. If a character sequence that
matches the syntax of a universal character name is produced by token
concatenation (6.10.3.3), the behavior is undefined. A #include preprocessing
directive causes the named header or source file to be processed from phase 1
through phase 4, recursively. All preprocessing directives are then deleted.
preprocessing token is converted into a token. The resulting tokens are
syntactically and semantically analyzed and translated as a translation unit.