在C语言的宏定义中,单井号(#)就是将后面的 宏参数 进行字符串操作,就是将后面的参数用双引号引起来,双井号(##)就是用于连接。
例如代码:
#define PRINT(NAME) printf("token"#NAME"=%d\n", token##NAME)调用时使用:
PRINT(9);宏展开即为:
printf("token"#9"=%d\n",token##9);#9即为"9",token##9即为: token9整个为:
printf("token""9""=%d\n",token9);之前定义过token9为9,所以就是输出 token9=9;解释到这里应该就明白单#和双#怎么用了。
#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)
void main(){
int token9=9;
PRINT(9);
struct command commands[] = {
COMMAND(quit),
COMMAND(help),
};
commands[0].function();
}REF:
1,http://www.cnblogs.com/cyttina/archive/2013/05/11/3072969.html
本文详细介绍了C语言中宏定义的使用方法,包括单井号和双井号的作用,通过实例展示了如何在实际代码中应用宏定义,并提供了一个包含宏定义、变量和函数调用的完整程序示例。

2万+

被折叠的 条评论
为什么被折叠?



