可能的重复:
当我们定义宏时,do while(0)有什么用处?
为什么C / C ++宏中有时会出现无意义的do / while和if / else语句?
C多行宏:do / while(0)vs作用域块
我之前看到过很多像这样的用法,以前我认为程序员想要轻松地跳出一段代码块。为什么我们在这里需要一个 do { ... } while (0) 循环呢?我们是在试图告诉编译器什么吗?
例如,在Linux内核2.6.25中,include/asm-ia64/system.h
/*
* - clearing psr.i is implicitly serialized (visible by next insn)
* - setting psr.i requires data serialization
* - we need a stop-bit before reading PSR because we sometimes
* write a floating-point register right before reading the PSR
* and that writes to PSR.mfl
*/
#define __local_irq_save(x) \
do { \
ia64_stop(); \
(x) = ia64_getreg(_IA64_REG_PSR); \
ia64_stop(); \
ia64_rsm(IA64_PSR_I); \
} while (0)