Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!olivea!tymix!cirrusl!sunfire!dhesi From: dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) Newsgroups: comp.lang.c Subject: Re: TRUE and FALSE Message-ID: <2342@cirrusl.UUCP> Date: 31 Aug 90 01:57:23 GMT References: <11215@alice.UUCP> <514@demott.COM> <2316@cirrusl.UUCP> <1990Aug29.153917.28110@warwick.ac.uk> Sender: news@cirrusl.UUCP Lines: 35 In <1990Aug29.153917.28110@warwick.ac.uk> asrap@warwick.ac.uk (Sean Legassick) explains why he might want to use enum {false, true} for booleans in C and continues: >I would be interested in anyone who thinks an int declaration of >a boolean variable is clearer. It depends. Try this: typedef enum {false, true} bool; ... while ((c = getc(stdin)) != EOF) { bool got_digit; got_digit = isdigit(c); ... if (got_digit == true) { ... } } Since isdigit() is not guaranteed to return 0 or 1, the value of got_digit could be something else (e.g. 4). Then, even when got_digit is true by C conventions (nonzero), it isn't necessarily equal to its enumeration value "true". No matter what you do, you cannot in C get around this: Although the result of a boolean condition is always 0 or 1, all nonzero values are considered to be true in a boolean context. All mechanisms that try to represent this fact using only two values (e.g. TRUE and FALSE, or enum {false, true}) are likely to lead to bad code that looks good. -- Rahul Dhesi UUCP: oliveb!cirrusl!dhesi