4 ms·
No, the relational operators (<, >, <= and >=) are defined to evaluate to either 0 or 1, and the result has type int. The same is true of the equality operator
by caf 12y ago
No, the relational operators (<, >, <= and >=) are defined to evaluate to either 0 or 1, and the result has type int.
The same is true of the equality operators (= and !=), the logical negation operator (!), and the logical boolean operators (&& and ||).
There are other C idioms based on this, like !!expr to squash a zero-or-non-zero expression down to zero-or-one.
- mehrdada 12y agoRegardless of the correctness of the code, if it's not obvious and you need to explain it, you probably should not write it (unless you have profile data that makes you do otherwise). "Programs must be written for people to read, and only incidentally for machines to execute."
- caf 12y agoI think that particular example should be obvious to anyone whose C isn't "a little rusty".