Why do I often see int instead of bool returned by Boolean functions in C and C++?

Posted on 07/09/2018 by Ken Gregg

There are various reasons for using the int data type to represent Boolean return values, function parameters, or variables in C and C++. Some are historical and traditional.

Boolean types in C

Before the C99 standard, there was no official Boolean data type in the C language. A few compilers implemented their own approaches, but an int was typically used for representing true and false values, where false was represented by zero and true was represented by any non-zero value.

There are many examples of functions and macros in the C standard library that use this convention. So, that’s how a lot of C code was written. Occasionally, some developers or teams would “roll their own” bool data type by setting up a typedef and some true and false constants. But most folks just used int.

C has officially had the _Bool data type since C99, along with a bool typedef when you include <stdbool.h>. The reasons to use int instead of bool in C code include:

  • Plain old tradition
    This is the way it was done from the beginning of C, and if it was good enough then, it’s good enough now.
  • Consistency with the standard library
    Many functions in the C standard library have always and will always use int to report Boolean results
  • Consistency with existing in-house or third-party libraries.
  • Force of habit
    You’d have to use bool 21 times before breaking this habit, according to habitual experts.
  • Lack of awareness
    The developer might not be aware of the bool typedef (and _Bool data type) in C, even though it has been there since C99.
  • Source code portability
    There might be a requirement for source code portability to compilers which don’t yet support the C99 _Bool data type. (Yes, there are many compilers out there that still don’t support all of the C99 features, even nearly two decades later. And some may never do so.)
  • Typing fatigue syndrome (TFS)
    It’s apparently a thing, although it isn’t covered by most insurance plans. Including <stdbool.h> is just too much typing for the overworked, overtired fingers of the developer.

Boolean types in C++

C++ has had a bool data type (and keywords for true and false) since the first C++ standard in 1998. It was discussed by Stroustrup in 1994 as a feature accepted by the ANSI/ISO committee, in his book The Design and Evolution of C++. If you’re writing in C++, the reasons to use int instead of bool likely include:

  • Lack of awareness
    The developer might not be aware of the bool data type in C++, even though it has been there since the first standard, C++98.
  • Force of habit
    Again, you often see this in developers who grew up using int in C, and then moved to C++. Or the habit was formed in C++ while writing C++ code before bool was introduced (i.e., prior to language standardization).

What to do?

Many of the coding standards I’ve dealt with in my clients’ organizations will say something like:

For new code, use bool to represent Boolean variables, formal parameters, and return values. For existing code that uses int to represent Boolean values, don’t convert the code to use bool.

From a readability/maintainability perspective, I advocate the use of bool in both C and C++. Using bool makes it crystal clear what the possible values are, without having to consult function documentation, etc.

Using int opens up a wide range of possible values, and it may not be immediately clear to the reader of the code that there are really only two potential values. And even if your use of int for a Boolean value is clear to you, there might be a maintainer of the code down the road who decides to start returning other numeric values. So, if your intent is Boolean, use bool.

bool boolean data type c99 return data type return value _bool best practice software development software engineering programming c and c++ languages programming in c programming in c++ c c++ c and c++