The relational and equality operators are left-associative in C. Consequently C, unlike many other languages, allows chaining of relational and equality operators. C99, section 6.5.8 "Relational operators", paragraph 6 has a footnote (92) which says:
The expression a<b<c is not interpreted as in ordinary mathematics. As the syntax indicates, it means (a<b)<c; in other words, "if a is less than b, compare 1 to c; otherwise, compare 0 to c".
These operators are left-associative, which means the leftmost comparison is performed first, and the result is compared with the rightmost comparison not non-associative as they often are in other languages. A comparison such as{{x<=y<=z}}, for example, is equivalent to (x<=y ? 1 : 0) <= z
, which is a different interpretation from that of ordinary mathematical notation. This allows a programmer to write an expression (particularly an expression used as a condition) that can be easily misinterpreted.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP09-A | 1 (low) | 1 (unlikely) | 2 (medium) | P2 | L3 |
References
Wiki Markup |
---|
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.5.8 "Relational operators" |
...
03. Expressions (EXP) EXP14-C. Beware of integer promotion when performing bitwise operations on chars or shorts