gpt4 book ai didi

c - 如何理解在宏中有# 或## 的情况下重新扫描替换 token 序列以获得更多定义的标识符?

转载 作者:太空宇宙 更新时间:2023-11-04 01:22:24 26 4
gpt4 key购买 nike

来自 KRC 的 The C Programming Language,关于宏定义中的运算符 # 和 ##

Two special operators influence the replacement process. First, if an occurrence of a parameter in the replacement token sequence is immediately preceded by #, string quotes (") are placed around the corresponding parameter, and then both the # and the parameter identifier are replaced by the quoted argument. A \ character is inserted before each " or \ character that appears surrounding, or inside, a string literal or character constant in the argument.

Second, if the definition token sequence for either kind of macro contains a ## operator, then just after replacement of the parameters, each ## is deleted, together with any white space on either side, so as to concatenate the adjacent tokens and form a new token. The effect is undefined if invalid tokens are produced, or if the result depends on the order of processing of the ## operators. Also, ## may not appear at the beginning or end of a replacement token sequence.

In both kinds of macro, the replacement token sequence is repeatedly rescanned for more defined identifiers. However, once a given identifier has been replaced in a given expansion, it is not replaced if it turns up again during rescanning; instead it is left unchanged.

我无法理解最后一段,尤其是加粗的句子。

您能否改写一下,和/或举一些例子?谢谢。

最佳答案

考虑片段:

#define A   B + C
#define B 1
#define C 2

int k = A;

在这种情况下第一次通过

将替换A:

int k = B + C;

第二遍将替换 BC

int k = 1 + 2;

现在考虑另一个片段:

#define A   B + C
#define B A
#define C A

int k = A;

现在第一遍将展开A一次,和以前一样:

int k = B + C;

第二个将像以前一样替换 BC:

int k = A + A;

但是这里它会停止,因为 A 在第一遍之前已经展开了。

关于c - 如何理解在宏中有# 或## 的情况下重新扫描替换 token 序列以获得更多定义的标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38975829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com