The relational and equality operators are left-associative in C. Consequently, C, unlike many other languages, allows chaining of relational and equality operators. Subclause 6.5.8, footnote 107, of the C Standard [ISO/IEC 9899:2011], says:
a<b<cis not interpreted as in ordinary mathematics. As the syntax indicates, it means
(a<b)<c; in other words, "if
ais less than
b, compare 1 to
c; otherwise, compare 0 to
These operators are left-associative, which means the leftmost comparison is performed first, and the result is compared with the rightmost comparison. This syntax allows a programmer to write an expression (particularly an expression used as a condition) that can be easily misinterpreted.
Noncompliant Code Example
Although this noncompliant code example compiles correctly, it is unlikely that it means what the author of the code intended:
a < b < c evaluates to true rather than, as its author probably intended, to false, and the expression
a == b == c evaluates to false rather than, as its author probably intended, to true.
Treat relational and equality operators as if it were invalid to chain them:
Incorrect use of relational and equality operators can lead to incorrect control flow.
C3392, C3401, C4111, C4112, C4113
|LDRA tool suite|
|433 S||Fully implemented|
|Polyspace Bug Finder|
|CERT C: Rec. EXP13-C||Checks for possibly unintended evaluation of expression because of operator precedence rules (rec. fully covered)|
|SEI CERT C++ Coding Standard||VOID EXP17-CPP. Treat relational and equality operators as if they were nonassociative|
|[ISO/IEC 9899:2011]||Subclause 6.5.8, "Relational Operators"|