宏 #,##,_ _VA_ARGS_ _

宏里面使用:

一、#  转为字符串

#define PSQR(x) printf("the square of" #x "is %d. ",(x)*(x))         如果使用PSQR("test2”),则编译出错;而使用PSQR(test2),则ok;

#define TEST2(p) (cout<<#p<<endl);    如果TEST2("test2");  输出”test2“。奇怪?

二、##  连接两个参数

#define XNAME(n) x##n,如果n为a,则展开后为xa。

三、_ _VA_ARGS_ _,C99引入的。

#define PR(...) printf(_ _VA_ARGS_ _)
#define qWiFiDebug(format, ...) printf("[WiFi] "format" File:%s, Line:%d, Function:%s", ##__VA_ARGS__, __FILE__, __LINE__ , __FUNCTION__);
#define pr_err(format, ...) fprintf(stderr, format, ## __VA_ARGS__)
宏前面加上##的作用在于,当可变参数的个数为0时,这里的##起到把前面多余的","去掉的作用,否则会编译出错

#define XNAME(n) x##n,如果n为a,则展开后为xa。
原文地址:https://www.cnblogs.com/lidabo/p/4570945.html