[previous] 3393 [next] Extra parentheses recommended. An arithmetic operation (* / + -) is the operand of a different operator with the same precedence.
Readability

The order in which operators are processed in this expression is not sufficiently clear and so additional parentheses are recommended. In the absence of parentheses, the sequence is determined by rules of precedence and associativity.

In this expression, there are 2 different arithmetic operators with the same precedence ((* /) or (+ -)) , and the order in which they are evaluated may not be obvious. In fact, the first operator (the left hand one) is evaluated first. Add parentheses to make this clear.

For example:


/*PRQA S 584,2017,3103,3198,3199,3203,3401,3408,3447 ++*/

extern int a;
extern int b;
extern int c;

extern void foo(void)
{
    int r;

    r = a * b / c;                    /* Message 3393 + 3390 */
    r = (a * b) / c;                  /* OK                  */

    r = a / b * c;                    /* Message 3393 + 3390 */
    r = (a / b) * c;                  /* OK                  */

    r = a + b - c;                    /* Message 3393 + 3390 */
    r = (a + b) - c;                  /* OK                  */

    r = a - b + c;                    /* Message 3393 + 3390 */
    r = (a - b) + c;                  /* OK                  */

}


MISRA-C:2004 Rules applicable to message 3393:

Rule  12.1  (Advisory) Limited dependence should be placed on C's operator precedence rules in expressions.


See also:

QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents