Simon 's Blog
» 做笔记做笔记
Toggle navigation
Simon 's Blog
HOME
总裁介绍
coper
zongcai
what
ARCH
TAGS
navigation
!!! define 变参__VA_ARGS__,"#","##"的使用
? C语言 ?
2018-03-13 15:27:45
241
0
0
simon88
? C语言 ?
摘自[C 语言 define 变参__VA_ARGS__使用](https://www.cnblogs.com/langzou/p/6674528.html) #define中的"\#" "\#\#" - "\#":宏展开时候替换#后面紧跟的变量 - "\#\#":连接"##"前后的内容(变量) 例子: ```C #include "stdio.h" #define LOG_TYPE1(format, ...) do{ \ printf(format, __VA_ARGS__); \ } while(0) #define LOG_TYPE2(format, args...) do{ \ printf(format, args); \ } while(0) #define LOG_TYPE3(format, ...) do{ \ printf(format, ##__VA_ARGS__); \ } while(0) #define LOG_TYPE4(format, args...) do{ \ printf(format, ##args); \ } while(0) #define LOG(x) printf("LOG "#x" %d \n", x); int value = 10; int main() { //LOG_TYPE1("hello %d \n"); error LOG_TYPE1("hello %d \n", 1); //LOG_TYPE2("hello \n"); error LOG_TYPE2("hello %d \n", 2); LOG_TYPE3("hello 3\n"); LOG_TYPE3("hello %d\n", 3); LOG_TYPE4("hello 4\n"); LOG_TYPE4("hello %d\n", 4); LOG(10); LOG(value); return 0; } ``` 结果: ```LOG hello world. hello 1 hello 2 hello 3 hello 3 hello 4 hello 4 LOG 10 10 LOG value 10 ```
上一篇:
Linux Select之坑(转载)
下一篇:
DHT协议(翻译)
0
赞
241 人读过
新浪微博
微信
腾讯微博
QQ空间
人人网
提交评论
0
条评论
More...
<>