The precedence of operators within expressions should be made explicit
The precedence of operators within expressions should be made explicit.
The C language has a large number of operators and their precedence is not intuitive. Inexperienced programmers can easily make mistakes. Remove any ambiguity by using parentheses to explicitly define operator precedence.
The following table list the MISRA C® definition of operator precedence for this rule.
| Description | Operator and Operand | Precedence |
|---|---|---|
| Primary | identifier, constant, string literal, (expression) | 16 |
| Postfix | [] () (function call) . -> ++(post-increment) --(post-decrement) ()
{}(C99: compound literals) | 15 |
| Unary |
| 14 |
| Cast | () | 13 |
| Multiplicative | * / % | 12 |
| Additive | + - | 11 |
| Bitwise shift | << >> | 10 |
| Relational | <> <= >= | 9 |
| Equality | == != | 8 |
| Bitwise AND | & | 7 |
| Bitwise XOR | ^ | 6 |
| Bitwise OR | | | 5 |
| Logical AND | && | 4 |
| Logical OR | || | 3 |
| Conditional | ?: | 2 |
| Assignment | = *= /= += -= <<= >>= &= ^= |= | 1 |
| Comma | , | 0 |
Operand of logical %s is not a primary expression. The precedence of operators within expressions should be made explicit.
If you expect a rule violation but do not see it, refer to the documentation of Polyspace® Code Prover™ or Polyspace Code Prover Server™.
| Group: Expressions |
| Category: Advisory |
| AGC Category: Advisory |