Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ames!mailrus!ukma!rutgers!att!ihlpl!knudsen From: knudsen@ihlpl.ATT.COM (Knudsen) Newsgroups: comp.lang.c Subject: Re: Question about int result of relational operator Summary: Yes Message-ID: <7482@ihlpl.ATT.COM> Date: 2 Nov 88 20:45:56 GMT References: <787@gtx.com> Distribution: na Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 43 In article <787@gtx.com>, randy@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."