James Kuyper said:
All logical expressions in C indicate truth by having a result of 1.
For certain meanings of the phrase "logical expressions".
The <, <=, >, >=, ==, !=, !, &&, and || operators are all defined to
yield either 0 or 1. I believe that's an exhaustive list.
On the other hand, of course, a scalar expression used as a condition is
treated as false if it compares equal to 0, true otherwise.
The is*() functions declared in <ctype.h> are only defined to return 0
for false, and some unspecified non-zero value for true. The same can
potentially apply to any expression used as a condition, unless its
top-level operator is one of the 9 listed above *or* it's specifically
written to yield only 0 or 1.
So:
if (x < y) count ++; // ok
count += (x < y); // ok and equivalent to the above
if (isdigit(c)) count ++; // ok
count += !!isdigit(c); // ok and equivalent to the above
count += isdigit(c); // legal but *not* equivalent to the above
#define FALSE 0
#define TRUE 1 // useful if you don't have <stdbool.h>
int cond = isdigit(c);
if (cond == FALSE) ...; // works, but unnecessarily verbose
if (cond == TRUE) ...; // dangerous, fails if cond < 0 or cond > 1
if (cond) ...; // the best way
See also section section 9 of the comp.lang.c FAQ, <
http://www.c-faq.com/>.