可能的重复:
当我们定义宏时,do while(0)有什么用? https://stackoverflow.com/questions/923822/whats-the-use-of-do-while0-when-we-define-a-macro
为什么 C/C++ 宏中有时会出现无意义的 do/while 和 if/else 语句? https://stackoverflow.com/questions/154136/why-are-there-sometimes-meaningless-do-while-and-if-else-statements-in-c-c-macr
C 多行宏:do/while(0) 与作用域块 https://stackoverflow.com/questions/1067226/c-multi-line-macro-do-while0-vs-scope-block
我见过很多这样的用法,以前我以为程序员想轻松地打破一段代码。为什么我们需要一个 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)
它总是在宏中使用,因此调用后需要使用分号,就像调用常规函数时一样。
在你的例子中,你必须写
__local_irq_save(1);
while
__local_irq_save(1)
会导致有关缺少分号的错误。如果 do while 不存在,这种情况就不会发生。如果只是关于范围,一对简单的花括号就足够了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)