#和##的用法
1.'#' 用于转换字符串,可以是任意字符
2."##" 用于连接两边的符号,例如:a##b == ab, ab必须为一个为已声明变量; a##b() == ab(), ab()必须为一个已声明的函数.
举例:
#include <stdio.h>
#define AAA(a) #a
#define BBB(b,c) b##c
#define CCC(b,c) b##c()
void printtest(void)
{
printf("I'm print test\n");
}
int main(void)
{
char *bc = "This is test";
printf("%s\n", AAA(123456));
printf("%s\n", AAA(bc));
printf("%s\n", BBB(b,c));
CCC(print , test);
return 0;
}
常用场景1:(用于区分不同级别的打印信息)
#define err(format, arg...) printf("err : " format "\n" , ## arg)
#define info(format, arg...) printf("info: " format "\n" , ## arg)
#define warn(format, arg...) printf("warn: " format "\n" , ## arg)
常用场景2:(封装函数)
void module_info();
void module_length();
void module_load();
#define module_call(name) module_##name()
调用方法:
module_call(info);
module_call(length);
module_load(load);