c - 在 C 语言的翻译阶段 5 和 6 中,空格什么时候有意义?

标签 c language-lawyer

回顾一下,标准中描述了阶段 5-7:

  1. Each source character set member and escape sequence in character constants and string literals is converted to the corresponding member of the execution character set; if there is no corresponding member, it is converted to an implementation- defined member other than the null (wide) character. 7)
  2. Adjacent string literal tokens are concatenated.
  3. White-space characters separating tokens are no longer significant. Each preprocessing token is converted into a token. The resulting tokens are syntactically and semantically analyzed and translated as a translation unit.

现在我同意空白字符在第 7 阶段不再重要,但难道不能在第 4 阶段之后就摆脱它们吗?有没有一个例子表明这会有所作为?

当然应该意识到,删除分隔标记的空白字符在这个阶段不起作用,因为第 4 阶段之后的数据由预处理标记组成。这个想法是为了在早期阶段摆脱分隔预处理标记的空格。

最佳答案

考虑这个源代码

char* str = "some text"   " with spaces";

在第 5 阶段,它被转换为这些标记(每行一个标记):

char
*
str
=
"some text"    
" with spaces"

此处重要的是“some text”和“with spaces”中的空格。

之后所有标记之间的空格(见上文)都将被忽略。

如果您在第 5 步之前删除空格,您会得到其他字符串文字,例如“sometext”

关于c - 在 C 语言的翻译阶段 5 和 6 中,空格什么时候有意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31874715/

相关文章:

c - 使用字符串读取数字

java - HashCode - 如果相同的对象恰好散列在同一个桶中会发生什么?

c++ - 通过对其基类的引用移动对象后,对象的状态是什么?

c++ - 为什么 std::unique_ptr 没有 operator<<?

复杂的声明表达式问题

c - C 程序中非常大的函数的影响?

c - 如何添加/减去指向堆不同部分的指针(在 C 中)

c - 程序堆内存的初始大小

c++ - 这真的违反了严格的别名规则吗?

java - i == (i = 2) 的结果是什么?