Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: some code fragments are made mono-spaced.

...

When multiple statements are used in a macro, they should be bound together in a do-while loop syntactically, so the macro can appear safely inside inside if clauses or other places that expect a single statement or a statement block. (Alternatively, when an if, for, or while statement uses braces even for a single body statement, then multiple statements in a macro will expand correctly even without a do-while loop. See EXP19-C. Use braces for the body of an if, for, or while statement.

...

The problem is the semicolon (;) following the block.

Compliant Solution

Wrapping the macro inside a do-while loop mitigates the problem.

Code Block
bgColor#ccccFF
langc
/*
 * Swaps two values.
 * Requires tmp variable to be defined.
 */
#define SWAP(x, y) \
  do { \
    tmp = x; \
    x = y; \
    y = tmp; } \
  while (0)

The The do-while loop will always be executed exactly once.

...