C programmers commonly make errors regarding the precedence rules of C operators due to because of the unintuitive low-precedence levels of &
, |
, ^
, <<
, and >>
. Mistakes regarding precedence rules can be avoided by the suitable use of parentheses. Using parentheses defensively reduces errors and, if not taken to excess, makes the code more readable.
Section Subclause 6.5 of the C Standard defines the precedence of operation by the order of the subclauses.
...
EXP00-EX1: Mathematical expressions that follow algebraic order do not require parentheses. For instance, in the expression
Code Block |
---|
x + y * z
|
the multiplication is performed before the addition by mathematical convention. Consequently, parentheses to enforce the algebraic order would be redundant:
...
Mistakes regarding precedence rules may cause an expression to be evaluated in an unintended way. This , which can lead to unexpected and abnormal program behavior.
...
[Dowd 2006] | Chapter 6, "C Language Issues" ("Precedence," pp. 287–288) |
[ISO/IEC 9899:2011] | Section Subclause 6.5, "Expressions" |
[Kernighan 1988] | |
[NASA-GB-1740.13] | Section 6.4.3, "C Language" |
...