C语言中不常用却很实用的用法
“##”连字符
例子:
#define LWIP_MEMPOOL(name,num,size,desc) MEMP_##name,
解析:上面宏定义中的“##”为连接符,用于连接两个Token为一个Token。
上面宏在使用时有
LWIP_MEMPOOL(RAW_PCB, MEMP_NUM_RAW_PCB, sizeof(struct raw_pcb), "RAW_PCB")
被编译器解读成
MEMP_RAW_PCB,
“#”字符串化
例子
#include <stdio.h>
#define ERROR_IF(expression) {if(expression)printf("error:"#expression"\n");}
int main(void)
{
int a = 1,b = 2;
ERROR_IF(a != b);
return 0;
}
输出
li@a:~$ ./a.out
error:a != b
以后碰到会持续更新