Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

While inline functions are, in general, more suitable for this task (see PRE00-A. Prefer inline or static functions to function-like macros), occasionally they are not feasible . For instance, (when macros are expected to operate on variables of different types, for example).

When multiple statements are used in a macro, they should be bound together in a loop syntactically, so the macro can appear safely inside if-clauses, or other places that expect a single statement or a statement block.

...

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

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

...