相关疑难解决方法(0)

适当的C预处理器宏无操作

对于调试日志记录,我经常看到并使用类似的东西

#ifdef DEBUG
    #define DLOG(fmt, args...) printf("%s:%d "fmt,__FILE__,__LINE__,args)
#else
    #define DLOG(fmt, args...)
#endif
Run Code Online (Sandbox Code Playgroud)

但在很多地方,我看到第二个#define被替换了

#define DLOG(fmt, args...) do {} while (0)
Run Code Online (Sandbox Code Playgroud)

特别是,有这个答案,对同一问题的另一个答案的评论表明,问题将出现在像

if (condition)
    DLOG("foo");
Run Code Online (Sandbox Code Playgroud)

虽然我的快速测试表明,行中产生的分号本身将作为条件内的无操作语句.

是一个或其他的什么do {} while (0)好?如果是这样,为什么?还有其他更好的东西吗?

c macros c-preprocessor

7
推荐指数
2
解决办法
4593
查看次数

标签 统计

c ×1

c-preprocessor ×1

macros ×1