通常的做法是定义字符串化宏,例如:
#define str(token) #token
#define xstr(token) str(token)
为什么通常使用 x
前缀? “x”有什么意义吗?
(我猜它可能代表“扩展”(如果有的话),但我没有证据支持这一点。)
最佳答案
正如其他人所建议的那样,x
似乎用于extended 或者我也看到过expanded。关于 Stringizing 的文章GNU 引用了这种用法。
他们将其表述为:
"If you want to stringize the result [...], you have to use two levels of macros."
#define xstr(s) str(s)
#define str(s) #s
#define foo 4
str (foo)
→ "foo"
xstr (foo)
→ xstr (4)
→ str (4)
→ "4"
他们继续声明:
"s is stringized when it is used in str, so it is not macro-expanded first. But s is an ordinary argument to xstr, so it is completely macro-expanded before xstr itself is expanded (see Argument Prescan). Therefore, by the time str gets to its argument, it has already been macro-expanded."
虽然这些都不是具体的,但我认为您可以安全地假设 x 是扩展/扩展用法。
关于c - 典型的 xstr 宏中的 "x"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48420780/