C Style
Barry Margolin
barmar at think.COM
Sat Apr 23 08:29:04 AEST 1988
In article <3583 at haddock.ISC.COM> karl at haddock.ima.isc.com (Karl Heuer) writes:
>>There are times when a var can very intentionally have a non-zero (true)
>>value other than TRUE (1). The classic kind of case of this is [isalpha].
>
>Then isalpha() is not a boolean, in my book. I'm not convinced there's any
>good reason for this; it would be trivial to rewrite the function to return a
>true boolean. In most instances this would have zero run-time cost.
Well, if you're coding in C, you'll have to live with C's quirks.
Nowhere does the C language specify that any particular non-zero value
should be returned by the isXXX functions to indicate truth. The
person who wrote isalpha() might have done #define TRUE 2, and you
might have done #define TRUE 1. Who is to say that you are right and
he is wrong? The only thing that C specifies is that you must both do
#define FALSE 0. Remember, C doesn't have real booleans, and #define
TRUE 1 isn't going to change that fact.
I can think of one reason, however, why 1 should be used as a standard
truth value. Single-bit fields in structures are generally used as
flags, and it would be nice to be able to say flag = isXXX(...) rather
than flag = (isXXX(...) != FALSE). But this is just a wish; portable
code must currently use the more verbose version.
Barry Margolin
Thinking Machines Corp.
barmar at think.com
uunet!think!barmar
More information about the Comp.lang.c
mailing list