变参宏

#define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)

Here ‘...’ is a variable argument. In the invocation of such a macro, it represents the zero or more tokens until the closing parenthesis that ends the invocation, including any commas. This set of tokens replaces the identifier __VA_ARGS__ in the macro body wherever
it appears. 

等价于:
#define debug(format, args...) fprintf (stderr, format, args)

 

但是,如果变参的个数为0, compiler will complain。

debug(“what is this”);   //error,

debug(“what is this”,);  //right 这样的代码,太让人郁闷了

 

一个解决办法:

#define debug(format, ...) fprintf (stderr, format, ##__VA_ARGS__)




typedef enum _NF_MSG_TYPE_T

{
    ERROR = 0,      /**< Error messages.*/
    WARNING,        /**< Warnings.*/
    INFO,           /**< Normal messages.*/
    INVALID,        /**< Invalid type.*/
} NF_MSG_TYPE_T;




/**
@brief
NF_MSG


Define macro which could print out message in both DEBUG and RELEASE version.
*/
#define NF_MSG(type, format, args...) \
{ \
    if (ERROR == type) \
    { \
        fprintf (stderr, "ERROR:"); \
    } \
    else if (WARNING == type) \
    { \
        fprintf (stderr, "WARNING:"); \
    } \
    else if (INFO == type) \
    { \
        fprintf (stderr, "INFO:"); \
    } \
    else \
    { \
    } \
    fprintf (stderr, format, ##args); \
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值