奇怪的C宏语法(#var)

时间:2009-08-31 17:42:54

标签: c syntax macros

#符号在#define宏中用作变量前缀时的含义是什么?

例如,

#define my_setopt(x,y,z) _my_setopt(x, 0, config, #y, y, z)

2 个答案:

答案 0 :(得分:7)

它是Stringizing Operator,它将宏参数转换为字符串文字。

所以在你的例子中:

my_setopt(1, 2, 3)

将扩展为:

_my_setopt(1, 0, config, "2", 2, 3)

答案 1 :(得分:2)

#引用了这个表达式。例如:

#define SHOW(BAR) printf("%s is %d\n", #BAR , BAR)
SHOW(3+5);  // prints: 3+5 is 8