...
Non-Compliant Code Example
It is common for C programmers to assign the return value of a function while concurrently checking for and in-band error condition.
Code Block |
---|
|
if(ret = foo() != err) {
/* use ret */
}
|
However, since comparison binds tighter than assignment, the value of foo() != err
is stored in ret
. So if foo()
succeeds, ret
will always be set to 1, regardless of what foo()
actually returned.
Compliant Solution
Code Block |
---|
|
if((ret = foo()) != err) {
/* use ret */
}
|
Exceptions
EXP00-EX1: Mathematical expressions that follow algebraic order do not require parentheses. For instance, in the expression:
...
Wiki Markup |
---|
\[[Dowd 06|AA. C References#Dowd 06]\] Chapter 6, "C Language Issues" (Precedence, pp. 287-288)
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.5, "Expressions"
\[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "JCW Operator precedence/Order of Evaluation"
\[[MISRA 04|AA. C References#MISRA 04]\] Rule 12.1
\[[NASA-GB-1740.13|AA. C References#NASA-GB-1740.13]\] Section 6.4.3, "C Language" |
Related Rules and Recommendations
Navigation Map |
---|
| order-of-op |
---|
| order-of-op |
---|
cellWidth | 700 |
---|
wrapAfter | 1 |
---|
cellHeight | 15 |
---|
|
...
03. Expressions (EXP) 03. Expressions (EXP) EXP01-A. Do not take the size of a pointer to determine the size of the pointed-to type