It's common to wrap multi-statement macros in a do { ... } while(false) block. This is done to make the macro safe when used inside control structures like if without braces; without it, macros that expand to multiple statements can behave in unexpected ways.
For example, say I define a macro like this:
#define my_macro do_something(); do_something_else();And then I use it like this:
if (condition)
my_macro;That expands to:
if (condition)
do_something();
do_something_else(); // ← this part is **not** inside the if block!This leads to a bug, since do_something_else() always runs, regardless of the condition.
By wrapping the macro like this:
#define my_macro do { do_something(); do_something_else(); } while (false)Now when it's used:
if (something)
my_macro;It expands to:
if (something)
do { do_something(); do_something_else(); } while (false);This behaves as expected: everything stays inside the if.
The do { ... } while(false) pattern makes macros act like a single statement!