宏定义中使用可变参数
C99 增加了宏定义中使用可变参数的支持。
用法为在定义中通过'...'定义可变参数,后面通过__VA_ARGS__引用。
如下面定义DGB宏,在log中自动增加'DEBUG'。
#define DBG(format, ...) printf("DEBUG: " #format "\n", ##__VA_ARGS__)
宏使用例
DBG("%d - %s", a,b);
问题
如下不指定参数使用,则会编译失败
DBG("hahaha");
: error: expected expression before ‘)’ token
#define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__)
^
解决方法
#define DBG(format, ...) printf("DEBUG: " #format "\n", ##__VA_ARGS__)
^^
参考
https://stackoverflow.com/que...
http://gcc.gnu.org/onlinedocs...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。