Site Tools


do-while-0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
do-while-0 [February 18, 2026 at 12:46] – created yanevskivdo-while-0 [May 14, 2026 at 11:38] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +# do { ... } while (0)
 +In C programming language,`do { ... } while (0)` is a loop which does a single iteration. A compiler will optimize it away completely, so the syntax produces no actual machine code.
 +
 +
 +Most commonly, it's used to make [[function-macro|function-macros]].
 +
 +Example:
 +```c
 +/*
 + * - 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)
 +
 +```
 +
 +## Links
 +
 + - https://stackoverflow.com/questions/2381300/what-does-do-while-0-do-exactly-in-kernel-code
 +