Question about int result of relational operator

Knudsen knudsen at ihlpl.ATT.COM
Thu Nov 3 06:45:56 AEST 1988


In article <787 at gtx.com>, randy at gtx.com (Randy D. Miller) writes:
> I've seen conflicting information about the int result of a relational
> operator in which the condition is satisfied.  Does it evaluate to
> exactly one, or just "non-zero" ?  E.g., can we test whether two
> comparisons have the same relational result with the following ?

>    int a,b,c,d;
>    if ((a > b) == (c > d)) { 
>       /* both tests have the same relational result */
>    }

Last year I had the same doubts you did, and asked the same question
in a roundabout way.  I was assured on the net (and by K&R)
that Boolean TRUE is indeed exactly 1, not -1, 3, 7, 0xFF or whatever.

Very nice, because you can pretty freely intermix bitwise and
Boolean operations, at least below the top level of nested
expressions.  Your example could be done:
	if( !((a > b) ^ (c > d)) {...}
Yes, you now have exclusive OR.
Also, things like
	boolvar &= boolex;
and
	boolvar |= boolex;

Then there is the odd-number tester:
#define odd(n)  (n & 1)

But don't use ~ for ! (twiddle for bang) or you'll
get 0xFFFF for TRUE, but much worse 0xFFFE for FALSE -- not good.

Also be very wary of functions whose result is often treated as
Boolean, but is really integer or pointer so that "true" returns
as anything nonzero.  "Cast" these into Boolean if needed by
	!!fcn(...)

BTW, I've always considered C to be schizoid wrt the Boolean type;
there isn't any such type, but there are operators for it
and you can assign them as integers!
Well not exactly; && and || are really flow-control constructs.
-- 
Mike Knudsen  Bell Labs(AT&T)   att!ihlpl!knudsen
"Lawyers are like nuclear bombs and PClones.  Nobody likes them,
but the other guy's got one, so I better get one too."



More information about the Comp.lang.c mailing list