![]() |
|
MISRA-C:2004 Rule 12.1: (Advisory) |
|
||||
![]() | |||||||
The following guidelines are suggested in deciding when parentheses are required:
* no parentheses are required for the right-hand operand of an assignment operator unless the right-hand side itself contains an assignment expression:
x=a+b;/* acceptable */x=(a+b);/* () not required */
* no parentheses are required for the operand of a unary operator:
x=a*-1;/* acceptable */x=a*(-1);/* () not required */
* otherwise, the operands of binary and ternary operators shall be cast-expressions (see section 6.3.4 of ISO/IEC 9899:1990 [2]) unless all the operators in the expression are the same.
x=a+b+c;/* acceptable, but care needed */x=f(a+b, c);/* no () required for a + b */x=(a==b)?a:(a-b);if(a&&b&&c)/* acceptable */x=(a+b)-(c+d); x=(a*3)+c+d; x=(uint16_t)a+b;/* no need for ( ( uint16_t ) a ) */
* even if all operators are the same, parentheses may be used to control the order of operation. Some operators (e.g. addition and multiplication) that are associative in algebra are not necessarily associative in C. Similarly, integer operations involving mixed types (prohibited by several rules) may produce different results because of the integral promotions. The following example written for a 16-bit implementation demonstrates that addition is not associative and that it is important to be clear about the structure of an expression:
uint16_t a=10U; uint16_t b=65535U; uint32_t c=0U; uint32_t d; d=(a+b)+c;/* d is 9; a + b wraps modulo 65536 */d=a+(b+c);/* d is 65545 *//* this example also deviates from several other rules */
Note that Rule is a special case of this rule applicable solely to the logical operators, && and ||.
QAC messages that encompass this guideline:
| 3389 | Extra parentheses recommended to clarify the ordering of a % operator and another arithmetic operator (* / % + -). |
| 3391 | Extra parentheses recommended. A conditional operation is the operand of another conditional operator. |
| 3392 | Extra parentheses recommended. A shift, relational or equality operation is the operand of a second identical operator. |
| 3393 | Extra parentheses recommended. An arithmetic operation (* / + -) is the operand of a different operator with the same precedence. |
| 3394 | Extra parentheses recommended. A shift, relational or equality operation is the operand of a different operator with the same precedence. |
| 3395 | Extra parentheses recommended. A * or / operation is the operand of a + or - operator. |
| 3396 | Extra parentheses recommended. A binary operation is the operand of a conditional operator. |
| 3397 | Extra parentheses recommended. A binary operation is the operand of a binary operator with different precedence. |
|
||
(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 |