Dr. Roger Ianjamasimanana

The stdbool.h header for boolean data types in C

By Dr. Roger Ianjamasimanana

1. Overview of stdbool.h in C

The C99 standard brought in a straightforward way to represent logical values through a type known as bool. Variables of this type can hold either true or false. Prior to C99, developers commonly used integers (0 meaning “false” and non-zero meaning “true”) or relied on enumerations to mimic a boolean type.

To maintain compatibility with existing C90 (and earlier) programs, the C99 standard placed the definition of bool, true, and false in a separate header called stdbool.h. This means that if you want to use bool, you have to include the following in your C programs:


#include <stdbool.h>  // Provides bool, true, and false

After including stdbool.h, you can write code such as:

#include <stdbool.h>

  int main(void)
  {
      bool flag = true;         // Initialize to true
      if (flag)
      {
          // This block will execute because flag is true
      }
      
      flag = false;             // Now set it to false
      if (!flag)
      {
          // This block will execute because flag is false
      }
  
      return 0;
  }
  

In the code above:

  • bool flag = true; declares a boolean variable named flag and sets it to true.
  • if (flag) will succeed because flag is true.
  • Setting flag = false; and checking if (!flag) means you’re checking if flag is not true (i.e., false).

Even though bool offers a more direct way to represent logical states, it still fundamentally behaves like an integer behind the scenes. As a result, it is technically possible to assign any integer value to a bool variable (though that is not considered good practice).

2. The built-in type _Bool

C99 also introduced a built-in type called _Bool, which can hold either 0 or 1. stdbool.h uses _Bool as the underlying type for bool. If you do not include stdbool.h, you could still work directly with _Bool, but you would lose the convenience of the bool, true, and false macros.

Additionally, stdbool.h provides a macro constant for checking whether bool is indeed available in your compiler, typically by using preprocessor conditions related to the C99 standard.

3. Example program 1: simple feature toggle

#include <stdio.h>
#include <stdbool.h>

int main(void)
{
    bool feature_on = true;

    if (feature_on)
    {
        printf("Feature is ON.\n");
    }
    else
    {
        printf("Feature is OFF.\n");
    }

    // Turn feature off
    feature_on = false;

    if (!feature_on)
    {
        printf("Feature is now disabled.\n");
    }

    return 0;
}

In this program, we manage a simple “feature toggle” using a bool. The if statements check whether the toggle is true or false and print the corresponding status.


4. Example program 2: validations and checks

Below is a more practical scenario where boolean values can help with clarity:

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>  // for isalpha, isdigit, etc.

bool is_valid_password(const char *password);

int main(void)
{
    const char *testPass = "P@ssw0rd123";
    if (is_valid_password(testPass))
    {
        printf("Password is valid.\n");
    }
    else
    {
        printf("Password is invalid.\n");
    }

    return 0;
}

bool is_valid_password(const char *password)
{
    // Simple example check: password must contain at least one letter
    // and at least one digit, and be at least 6 characters in length.
    bool has_letter = false;
    bool has_digit = false;
    int length = 0;

    while (*password != '\0')
    {
        if (isalpha((unsigned char)*password))
        {
            has_letter = true;
        }
        if (isdigit((unsigned char)*password))
        {
            has_digit = true;
        }
        length++;
        password++;
    }

    if (has_letter && has_digit && length >= 6)
        return true;
    else
        return false;
}

In this example:

  • We define bool variables has_letter and has_digit to represent checks within the password.
  • is_valid_password returns true if all conditions (one letter, one digit, length >= 6) are met, or false otherwise.
  • Using bool makes the code more understandable than manually using int values for true/false checks.

5. Remarks

  • Using bool, true, and false clarifies intent and enhances code readability.
  • In some older compilers, stdbool.h might not be available, but in most modern C compilers, it’s part of the standard library.
  • Underneath, bool is typically just an alias for _Bool, and true/false are macros for 1 and 0.
  • You can use boolean expressions in if, while, and so on, just as you would with integer conditions. However, using bool makes your intentions explicit.

In conclusion, stdbool.h provides a standardized and more readable way to represent boolean values in C. By using bool, true, and false, your code can become more self-documenting and less prone to confusion.

feature-top
Readers’ comment
feature-top
Log in to add a comment
🔐 Access