【发布时间】:2012-11-30 13:11:03
【问题描述】:
可能重复:
Do-While and if-else statements in C/C++ macros
What’s the use of do while(0) when we define a macro?
我经常看到这样的代码:
#define foo() do { xxx; yyy; zzz; } while (0)
为什么在这里使用 do-while 包装器?为什么不简单
#define foo() { xxx; yyy; zzz; }
?
编辑:删除分号。
【问题讨论】:
-
我打赌你宁愿看到
#define foo() do { xxx; yyy; zzz; } while (0)(没有尾随分号 - 这正是为什么......) -
在 SO 搜索栏中搜索
[c]while (0),你会得到很多答案。 -
其他帖子的链接确实回答了这个问题:gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html
-
@Lundin - 在其他帖子上查看用户:raghava 的答案(链接)。
-
@Lundin 接受答案的第四个代码块解决了问题的这一部分。
标签: c