首先说明的一点是,这个是百度面试的时候问的一个问题,处于把问题弄透的一个原则,在网络上找了好多资料,其中一个还是比较好的,原文链接:https://www.quora.com/What-is-the-purpose-of-using-do-while-0-in-macros。
The do/while(0) pattern seen in many if not most macros in the Linux kernel and elsewhere has a specific purpose: It is the only construct in C that lets you define macros that always work the same way, so that a semicolon after your macro always has the same effect, regardless of how the macro is used (with particular emphasis on the issue of nesting the macro in an if without curly-brackets).
Let's consider an example:
#define foo(x) bar(x); baz(x)
Say you used it like this:
foo(wolf);
This expands to: