下面的c 宏声明是什么意思?
What does following c Macro declaration mean?
如下c代码:
#define __xstr(s) __str(s)
#define __str(s) #s
#s 是什么意思?
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 提到的那样,“以两个下划线开头或下划线后跟一个大写字母的标识符是保留的” .
宏允许字符串化,即将宏参数转换为字符串:
__xstr(foo)
产生 "foo"
__xstr(4)
产生 "4"
和
#define BLA bar
__xstr(BLA)
产生 "bar"
您需要两级宏才能实现此目的,有关详细信息,请参阅此处:
如下c代码:
#define __xstr(s) __str(s)
#define __str(s) #s
#s 是什么意思?
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 提到的那样,“以两个下划线开头或下划线后跟一个大写字母的标识符是保留的” .
宏允许字符串化,即将宏参数转换为字符串:
__xstr(foo)
产生 "foo"
__xstr(4)
产生 "4"
和
#define BLA bar
__xstr(BLA)
产生 "bar"
您需要两级宏才能实现此目的,有关详细信息,请参阅此处: