Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!gatech!bloom-beacon!think!ames!oliveb!sun!gorodish!guy From: guy%gorodish@Sun.COM (Guy Harris) Newsgroups: comp.lang.c Subject: Re: Defining TRUE and FALSE Message-ID: <23052@sun.uucp> Date: Thu, 9-Jul-87 14:11:22 EDT Article-I.D.: sun.23052 Posted: Thu Jul 9 14:11:22 1987 Date-Received: Sun, 12-Jul-87 06:25:19 EDT References: <13851@watmath.UUCP> Sender: news@sun.uucp Lines: 59 Keywords: boolean, true, false > #define TRUE ( 0 == 0 ) > #define FALSE ( 1 == 0 ) Which can be replaced by #define TRUE 1 #define FALSE 0 in any C implementation that isn't horribly broken. From K&R, page 189: 7.6 Relational operators ... The operators < (less than), ..., all yield 0 if the specified relation is false and 1 if it is true. ... 7.7 Equality operators ... The == (equal to) and the != (not equal to) operators are exactly analogous to the relational operators except for their lower precedence. (Thus a People have noted that some programmers code things like > > if( my_func() == TRUE ) > > when my_func() returns a non-zero value that isn't necessarily the > same as TRUE. In such cases why not code > > if( my_func() != FALSE ) > > since anyone who declares FALSE to be anything other than zero is > going to run into a few problems. I presume that in this case "my_func()" returns something to be thought of as a Boolean, except that any non-zero value, not just 1, maps to TRUE. If the function doesn't return a value that is a Boolean (since C doesn't permit you to explicitly say that something is Boolean, this means that it is not intended to be thought of as Boolean), you shouldn't compare it against TRUE *or* FALSE; if you want to know if the return value from "my_func" is zero, compare it against zero. Guy Harris {ihnp4, decvax, seismo, decwrl, ...}!sun!guy guy@sun.com