Versions Compared

Key

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

Macros are frequently used to make source code more readable. Macro definitions, regardless of whether they expand to a single or multiple statements should not conclude with a semicolon. (See guideline recommendation PRE10-C. Wrap multi-statement macros in a do-while loop.) If required, the semicolon should be included following the macro expansion. Inadvertently inserting a semicolon at the end of the macro definition can unexpectedly change the control flow of the program.

Another way to avoid this problem is to prefer inline or static functions over function-like macros. (See also recommendation PRE00-C. Prefer inline or static functions to function-like macros.)

...

This noncompliant code example creates a macro definition for a for loop in the program. for loops should require braces, even if it contains only a single body statement. (See guideline recommendation EXP19-C. Use braces for the body of an if, for, or while statement.) This macro takes an integer argument, which is the number of times the loop should run. The programmer has provided a semicolon at the end of the macro definition by mistake.

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

CERT C++ Secure Coding Standard: PRE11-CPP. Do not conclude macro definitions with a semicolon

...