c - 下面的c 宏声明是什么意思?

标签 c macros

<分区>

如下c代码:

#define __xstr(s) __str(s)
#define __str(s) #s

#s 是什么意思?

最佳答案

它是 Stringification operator :

The # operator (known as the "Stringification Operator") converts a token into a string, escaping any quotes or backslashes appropriately.

Example:

#define str(s) #s

str(p = "foo\n";) // outputs "p = \"foo\\n\";"
str(\n)           // outputs "\n"

If you want to stringify the expansion of a macro argument, you have to use two levels of macros:

#define xstr(s) str(s)
#define str(s) #s
#define foo 4

str (foo)  // outputs "foo"
xstr (foo) // outputs "4"

最后一个示例中的代码与问题中的代码非常接近。请注意,正如 @KeithThompson 提到的,“以两个下划线开头,或一个下划线后跟一个大写字母的标识符是保留的”。

关于c - 下面的c 宏声明是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900174/

相关文章:

c++ - 为什么我在读取二进制文本时会得到 unicodeand 大数字?

python - 解析的部分评估

c - c struct中私有(private)变量的解释

python - 从 python 脚本在共享库上运行 gcov

c - 宽字符字符串文字

c - 为什么将这些宏函数转换为函数不能正常工作?

php - 如何在 Controller symfony 2 中渲染 Twig 宏

Java反编译和JNI

c++ - 如何在编译时判断函数是否静态

c - Macro中struct的问题定义