对于调试日志,我经常看到并使用类似恰当的C预处理宏无操作
#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");
尽管我的快速测试表明,由此产生的分号本身将作为条件内的no-op语句。
是一个或另一个没有和do {} while (0)
更好?如果是这样,为什么?还有什么更好的吗?
[do {...} while(0)它有什么好处?](http://stackoverflow.com/questions/257418/do-while-0-what-is-it-good -对于) – 2012-07-18 19:54:54