Versions Compared

Key

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

...

This noncompliant code example contains multiple, unbound statements.:

Code Block
bgColor#FFcccc
langc
/*
 * Swaps two values.
 * Requires tmp variable to be defined.
 */
#define SWAP(x, y) \
  tmp = x; \
  x = y; \
  y = tmp

...

This noncompliant code example inadequately bounds multiple statements.:

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

This macro fails to expand correctly in some case, such as the following example, which is meant to be an if statement with two branches:

...

Code Block
bgColor#FFcccc
langc
if (x > y) { /* Single-branch if-statement!!! */

  tmp = x;   /* The one and only branch consists */
  x = y;     /* of the block. */
  y = tmp;
}
;            /* emptyEmpty statement */
else         /* ERROR!!! "parse error before else" */
  do_something();

...

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)

...