Synopsis: | Use parentheses around macro and macro parameters |
Language: | C++ |
Severity Level: | 4 |
Category: | Preprocessor |
Description: |
If a macro can be used in an expression or the parameter passed to a macro can be an expression, use parenthesis around them. Consider a macro that calculates the square of number. // Not allowed !!! #define SQUARE(p) p * p When the macro is used as follows: SQUARE(5 + 2) it is expanded into: 5 + 2 * 5 + 2 The result is 17 instead of 49. To prevent such unexpected results, always use parentheses around the entire macro and macro parameters, as in: An exception to this rule is when a macro is defined as a single token, as in:// OK #define SQUARE(p) ((p) * (p)) // OK #define SOMETHING SOMETHING_ELSE |