#在英语里面叫做 pound
在C语言的宏定义中,一个#表示字符串化;两个#代表concatenate
在宏定义中,#的功能是将其后面的宏参数进行字符串化操作(Stringfication),即无论后面的宏变量是字符串还是常量,都当成字符来处理。
而## 被称为连接符(concatenator),用来将两个Token连接为一个Token。注意这里连接的对象是Token就行,而不一定是宏的变量。
举例如下:
#include <iostream> void quit_command(){ printf("I am quit command\n"); } void help_command(){ printf("I am help command\n"); } struct command { char * name; void (*function) (void); }; #define COMMAND(NAME) {#NAME,NAME##_command} #define PRINT(NAME) printf("token"#NAME"=%d\n", token##NAME) main(){ int token9=9; PRINT(9); struct command commands[] = { COMMAND(quit), COMMAND(help), }; commands[0].function(); }
得到的结果是:
token9=9
I am quit command