Boolean Operators Slighted in C

Doug Gwyn gwyn at brl-smoke.ARPA
Wed Apr 30 21:43:01 AEST 1986


In article <838 at ihwpt.UUCP> knudsen at ihwpt.UUCP (mike knudsen) writes:
>Has anyone else noticed that Boolean types and operators
>have less than full citizenship in C?  Examples:
>...
>(3) There is no boolean data type.  No big gripe;
>lots of us say "typedef short bool" in our .h files.

Technically, there are no true Boolean data types in C.
Ints (NOT shorts) are made to serve when a Boolean would
be called for.  I have always considered this a deficiency,
but (except for getting "lint" to cooperate) it can be
remedied by the programmer being careful to keep Boolean
usages distinct from integer usages.  For example, treat
the result of a relational expression as Boolean, not as
a 1 or a 0.  I find that carefully maintaining the
distinction contributes to code correctness and
maintainability.

I generally frown on C language extension via the
preprocessor (as in a recent Byte article), but I make an
exception for Booleans.  The same standard header file
that I mentioned a few days ago (that defines "void" for
machines that don't have it, etc.) contains the following:

typedef int	bool;			/* boolean data */
#define 	false	0
#define 	true	1



More information about the Comp.lang.c mailing list