This question already has answers here:
how does do{} while(0) work in macro?

(3个答案)



Why use apparently meaningless do-while and if-else statements in macros?

(9个答案)


7年前关闭。




用这种方式定义宏的原因是什么:
#define test_errno(msg) do{if (errno) {perror(msg); exit(EXIT_FAILURE);}} while(0)

我的意思是do{}while(0)背后的原因是什么?当然它只会做一次,零是常数,不能以某种方式变为非零,那么为什么要使用这样的构造呢?

最佳答案

它允许像

if (condition)
   test_errno(...);

不管有无牙套都可以正常工作。

10-02 02:13
查看更多