【发布时间】:2012-07-18 19:51:26
【问题描述】:
对于调试日志,我经常看到并使用类似的东西
#ifdef DEBUG
#define DLOG(fmt, args...) printf("%s:%d "fmt,__FILE__,__LINE__,args)
#else
#define DLOG(fmt, args...)
#endif
但在很多地方,我看到第二个 #define 被替换为
#define DLOG(fmt, args...) do {} while (0)
特别是this answer,this other answer 上对同一问题的评论表明问题将出现在类似的情况
if (condition)
DLOG("foo");
尽管我的快速测试表明,在该行中生成的分号本身将用作条件内的无操作语句。
nothing 和 do {} while (0) 哪个更好?如果是这样,为什么?还有什么更好的吗?
【问题讨论】:
标签: c macros c-preprocessor