Using boolean values in C

C doesn't have any built-in boolean types. What's the best way to use them in C?

Solutions

From best to worse:

Option 1 (C99 and newer)

#include <stdbool.h>

Option 2

typedef enum { false, true } bool;

Option 3

typedef int bool;
enum { false, true };

Option 4

typedef int bool;
#define true 1
#define false 0

#Explanation

  • Option 1 will work only if you use C99 (or newer) and it's the "standard way" to do it. Choose this if possible.
  • Options 2, 3 and 4 will have in practice the same identical behavior. #2 and #3 don't use #defines though, which in my opinion is better.

If you are undecided, go with #1!

A few thoughts on booleans in C:

I'm old enough that I just use plain ints as my boolean type without any typedefs or special defines or enums for true/false values. If you follow my suggestion below on never comparing against boolean constants, then you only need to use 0/1 to initialize the flags anyway. However, such an approach may be deemed too reactionary in these modern times. In that case, one should definitely use <stdbool.h> since it at least has the benefit of being standardized.

Whatever the boolean constants are called, use them only for initialization. Never ever write something like

if (ready == TRUE) ...
while (empty == FALSE) ...

These can always be replaced by the clearer

if (ready) ...
while (!empty) ...

Note that these can actually reasonably and understandably be read out loud.

Give your boolean variables positive names, ie full instead of notfull. The latter leads to code that is difficult to read easily. Compare

if (full) ...
if (!full) ...

with

if (!notfull) ...
if (notfull) ...

Both of the former pair read naturally, while !notfull is awkward to read even as it is, and becomes much worse in more complex boolean expressions.

Boolean arguments should generally be avoided. Consider a function defined like this

void foo(bool option) { ... }

Within the body of the function, it is very clear what the argument means since it has a convenient, and hopefully meaningful, name. But, the call sites look like

foo(TRUE);
foo(FALSE):

Here, it's essentially impossible to tell what the parameter meant without always looking at the function definition or declaration, and it gets much worse as soon if you add even more boolean parameters. I suggest either

typedef enum { OPT_ON, OPT_OFF } foo_option;
void foo(foo_option option);

or

#define OPT_ON true
#define OPT_OFF false
void foo(bool option) { ... }

In either case, the call site now looks like

foo(OPT_ON);
foo(OPT_OFF);

which the reader has at least a chance of understanding without dredging up the definition of foo.

A boolean in C is an integer: zero for false and non-zero for true.

See also Boolean data type, section C, C++, Objective-C, AWK.

Tags: C / Boolean

Similar questions

In C/C++ is it safe to use boolean operator short circuit for control flow without using computed boolean value?
A recurring pattern in a piece of inherited code is that a chain of functions is called and the chain aborted, as soon as one of the functions returns a certain value. This value shall then be used for following computation. For the sake of demonstration the breaking value is 0. Consider the following program: Note that the short circuited variant ...
How to change the boolean value of C to the boolean value of python on mac
I would like to code in c ,but use the value in python by compile the c source file to .so file.
Is Addition of Char and Boolean Expression results in Boolean result?
In the following function The statement s[i] + s[i] == c; results in either zero or one (Boolean result). My question: will the above expression be converted to boolean expression? It is considering the value of s[i]
Is -!(condition) a correct way to obtain a full-bitvector from a boolean (mask-boolean)?
In removing conditional branches from high-performance code, converting a true boolean to unsigned long i = -1 to set all bits can be useful. I came up with a way to obtain this integer-mask-boolean from input of a int b (or bool b) taking values either 1 or 0: To get the opposite value: Has anybody seen this construction before? Am I on to somethi...
How can I convert Python boolean object to C int (or C++ boolean) (Python C API)
I have a variable PyObject that I know is a Python bool. It either is True or False (eg. Py_True or Py_False). Now I would like to convert it to C++ somehow. Doing this with strings isn't so hard, there is a helper function for that - PyBytes_AsString that converts python string into C string. Now I need something like that for boolean (or int as t...
Swig how to define a typemap that converts a C typedefined Boolean to java boolean
I'm using swig to create a glue between C and Java (Android). I have defined a type Boolean in C and used like this : (MyGlue.h) Then in Java I want to do the following: the problem I have is that MyGlue.isRunning returns an int Can I make a swig typemap that converts an int to a boolean value? I have tried the following, but I did not fully grasp ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.