[previous] 3391 [next] Extra parentheses recommended. A conditional operation is the operand of another conditional operator.
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 conditional operators and the order in which they are evaluated may not be obvious. In fact, the second operator (the right hand one) is evaluated first. Add parentheses around this operator and its operands to make it clear which operator is evaluated first.

For example:


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

extern int a;
extern int b;
extern int c;
extern int d;
extern int e;

extern void foo(void)
{
    int r;

    r = a ? b : c ? d : e;        /* Message 3391 + 3390 */
    r = a ? b : (c ? d : e);      /* OK                  */

    r = a ? b ? c : d : e;        /* Message 3391 + 3390 */
    r = a ? (b ? c : d) : e;      /* OK                  */

}


MISRA-C:2004 Rules applicable to message 3391:

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