Table of contents
1. Overview of stdbool.h in C2. The built-in type _Bool
3. Example program 1: simple feature toggle
4. Example program 2: validations and checks
5. Remarks
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.
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).
_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.
#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.
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:
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.
bool makes the code more understandable than manually using int values for
true/false checks.
bool, true, and false clarifies
intent and enhances code readability.
stdbool.h might not be available, but
in most modern C compilers, it’s part of the standard library.
bool is typically just an alias for
_Bool, and true/false are macros for 1 and 0.
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.
Author
Dr. Roger Ianjamasimanana