Versions Compared

Key

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

...

The assert() macro is a convenient mechanism for incorporating diagnostic tests in code. (See MSC11-C. Incorporate diagnostic tests using assertions.) Expressions used with the standard assert macro should not have side effects. The behavior of the assert macro depends on the status of the NDEBUG preprocessor symbol. If NDEBUG is undefined, the assert macro is defined to evaluate its expression argument and abort if the result of the expression compares equal to 0. If NDEBUG is defined, assert is defined to expand to ((void)0). Consequently, any side effects resulting from evaluation of the expression in the assertion are lost in non-debugging versions of the code.

This rule is similar to EXP44-C. Do not use rely on side effects in operands to sizeof, _Alignof, or _Generic.

Noncompliant Code Example

...

This compliant solution follows the guidance of PRE00-C. Prefer inline or static functions to function-like macros by defining an inline function iabs() to replace the ABS() macro. Unlike the ABS() macro, which operates on operands of any type, the abs() function accepts arguments only of type int

...

Code Block
bgColor#ccccff
langc
#define ABS(x)   ({ __typeof (x) __tmp = x; __tmp < 0 ? -__tmp : __tmp; })

Note that relying on such extensions makes code nonportable and violates MSC14-C. Do not introduce unnecessary platform dependencies.

...