[previous] MISRA-C:2004  Rule  19.10:  (Required) [next] In the definition of a function-like macro each instance of a parameter shall be enclosed in parentheses unless it is used as the operand of # or ##.
Within a definition of a function-like macro, the arguments shall be enclosed in parentheses. For example define an abs function using:

#define abs( x ) ( ( ( x ) >= 0 ) ? ( x ) : -( x ) )

and not:

#define abs( x ) ( ( x >= 0 ) ? x : -x )

If this rule is not adhered to then when the preprocessor substitutes the macro into the code the operator precedence may not give the desired results.

Consider what happens if the second, incorrect, definition is substituted into the expression:

z = abs( a - b );

giving:

z = ( ( a - b >= 0 ) ? a - b : -a - b );

The sub-expression -a - b is equivalent to (-a)-b rather than -(a-b) as intended. Putting all the parameters in parentheses in the macro definition avoids this problem.


QAC messages that encompass this guideline:

3410 Macro parameter not enclosed in ().



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents