c - 典型的 xstr 宏中的 "x"是什么意思?

标签 c c-preprocessor naming-conventions

通常的做法是定义字符串化宏,例如:

#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/

相关文章:

c - 限制在程序集文件中使用 #define'd 函数/内联函数

c - String #define 返回一个随机整数

检查 C 预处理器列表中是否存在条目

java - Java 类名应该重复包名吗?

c - 以相反顺序打印文本文件中的行

c - 函数名转换为 uint32_t

sql - 您首选的 bool 值对是什么 : 1/0 Yes/No True/False?

c# - 接口(interface)和抽象类的描述性命名约定

c - 用C读取文件的问题

python - 如何使用 Python 和 ctypes 处理指向指针的指针