#define MY_PRINT(_format, ...) printf("MIME : %s" _format, __FUNCTION__, __VA_ARGS__);
我想在打印消息的末尾添加"\n
。
但是,
#define MY_PRINT_LN(_format, ...) MY_PRINT(_format "\n", ...)
有编译错误。如何在定义宏传递...
参数?
答案 0 :(得分:4)
您使用__VA_ARGS__
:
#define MY_PRINT_LN(_format, ...) MY_PRINT(_format "\n", __VA_ARGS__)
这是C99的标准化。
如果没有参数,你最后会有一个冗余的逗号。没有标准解决方案,但GCC(可能还有其他编译器)提供了扩展:
#define MY_PRINT_LN(_format, ...) MY_PRINT(_format "\n", ## __VA_ARGS__)
使用额外的##
,如果没有参数,则删除最后的逗号。