### Question 9.1

What is the right type to use for Boolean values in C? Why isn't it a standard type? Should I use `#define`s or enums for the true and false values?

C does not provide a standard Boolean type, in part because picking one involves a space/time tradeoff which can best be decided by the programmer. (Using an `int` may be faster, while using `char` may save data space. Smaller types may make the generated code bigger or slower, though, if they require lots of conversions to and from `int`.)

The choice between `#define`s and enumeration constants for the true/false values is arbitrary and not terribly interesting (see also questions 2.22 and 17.10). Use any of ``

    	#define TRUE  1	#define YES 1
    	#define FALSE 0	#define NO  0

    	enum bool {false, true};	enum bool {no, yes};

or use raw 1 and 0, as long as you are consistent within one program or project. (An enumeration may be preferable if your debugger shows the names of enumeration constants when examining variables.)

Some people prefer variants like

    	#define TRUE (1==1)
    	#define FALSE (!TRUE)

or define ``helper'' macros such as

    	#define Istrue(e) ((e) != 0)

These don't buy anything (see question 9.2; see also questions 5.12 and 10.2).

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995 // mail feedback