# C语言中的“#”和“##”

``````#include <stdio.h>
#define f(a,b) a##b
#define g(a)   #a
#define h(a) g(a)

int main()
{
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
return 0;
}
``````

``````12
f(1,2)
``````

`#a`会创建一个字符串：`"a"``a##b`会创建一个新的`token``ab`。关于`macro`的处理，参加下列解释：

An occurrence of a parameter in a function-like macro, unless it is the operand of # or ##, is expanded before substituting it and rescanning the whole for further expansion. Because g’s parameter is the operand of #, the argument is not expanded but instead immediately stringified (“f(1,2)”). Because h’s parameter is not the operand of # nor ##, the argument is first expanded (12), then substituted (g(12)), then rescanning and further expansion occurs (“12”).

This site uses Akismet to reduce spam. Learn how your comment data is processed.