...
The intent of the expression in this noncompliant code example is to test the least significant bit of x
.:
Code Block | ||||
---|---|---|---|---|
| ||||
x & 1 == 0 |
...
In this compliant solution, parentheses are used to ensure the expression evaluates as expected.:
Code Block | ||||
---|---|---|---|---|
| ||||
(x & 1) == 0 |
...
the multiplication is performed before the addition by mathematical convention. Consequently, parentheses to enforce the algebraic order would be redundant.:
Code Block | ||||
---|---|---|---|---|
| ||||
x + (y * z) |
...
CERT C++ Secure Coding Standard | EXP00-CPP. Use parentheses for precedence of operation |
ISO/IEC TR 24772:2013 | Operator Precedence/Order of Evaluation [JCW] |
MISRA - C:2012 | Rule 12.1 (advisory) |
Bibliography
[Dowd 2006] | Chapter 6, "C Language Issues" ("Precedence," pp. 287–288) |
[ISO/IEC 9899:2011] | Section 6.5, "Expressions" |
[Kernighan 1988] | |
[NASA-GB-1740.13] | Section 6.4.3, "C Language" |
...
...