[comp.lang.c] Question about int result of relational operator

randy@gtx.com (Randy D. Miller) (11/02/88)

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 ?
(Obviously there are other ways of doing this, but that is not the 
question here.)

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

-- 
Randy D. Miller             (602) 870-1696
GTX Corp., 8836 N. 23rd Ave., Phoenix, AZ 85021
{cbosgd,decvax,hplabs,amdahl,nsc}!sun!sunburn!gtx!randy

guy@auspex.UUCP (Guy Harris) (11/02/88)

>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" ?

It evaluates to exactly one in all C implementations.  (Implementations
where it does not may purport to be C implementations, but they aren't.)

crossgl@ingr.UUCP (Gordon Cross) (11/02/88)

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" ?

According to the proposed ANSI standard, the result of the operations
<, >, <=, >=, ==, !=, &&, and || has type "int" and a value of precisely
zero or one.  Hope that helps!


Gordon Cross
Intergraph Corp.  Huntsville, AL

gwyn@smoke.BRL.MIL (Doug Gwyn ) (11/03/88)

In article <787@gtx.com> randy@gtx.UUCP (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"?

Aaargh!  Looking in K&R (1st Ed.) in the index under "relational
expression, value of", I find that it's discussed on p.41.  Turning
to p.41 I find the answer.  And so could you.

knudsen@ihlpl.ATT.COM (Knudsen) (11/03/88)

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."

blm@cxsea.UUCP (Brian Matthews) (11/03/88)

Randy D. Miller (randy@gtx.UUCP) 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 */
|   }

From Section 3.3.8 "Equality operators" of the ANSI draft standard, last
paragraph under Semantics:

"Each of the operators < (less than), > (greater than), <= (less than or
equal to), and >= (greater than or equal to) shall yield 1 if the
specified relation is true and 0 if it is false.  The result has type
int."

From Section 3.3.9 "Equality operators", first paragraph under Semantics:

"The == (equal to) and the != (not equal to) operators are analogous to
the relational operators except for their lower precedence."

It is also guaranteed in K&R, but I don't have a K&R handy, so I can'
give you a section or page number.

-- 
Brian L. Matthews  blm@cxsea.UUCP   ...{mnetor,uw-beaver!ssc-vax}!cxsea!blm
+1 206 251 6811    Computer X Inc. - a division of Motorola New Enterprises

scjones@sdrc.UUCP (Larry Jones) (11/03/88)

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"?

The draft standard says "Each of the operators ... shall yield 1
if the specified relation is true and 0 if it is false."  I
believe K&R made a similar statement.

----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones@sdrc.uucp
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150                  AT&T: (513) 576-2070
"Save the Quayles" - Mark Russell

bill@twwells.uucp (T. William Wells) (11/06/88)

In article <7482@ihlpl.ATT.COM> knudsen@ihlpl.ATT.COM (Knudsen) writes:
: 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(...)

Clearer, and likely to generate better code, is:

	fcn != 0

---
Bill
{uunet|novavax}!proxftl!twwells!bill