In programming, you very often need a data type that can only have one of two possible values, such as:
For this, C provides Booleans. A boolean represents truth values. In C, booleans are not built-in from the very beginning like in Java or Python. Instead, C treats the integer 0 as false, and any non-zero integer (usually 1) as true. However, modern C (C99 standard and later) introduced a proper boolean type that is much more readable.
Before the C99 standard, C programmers exclusively used integers to represent boolean values.
0 represents False.1 (or any other non-zero number like 5, -10, etc.) represents True.#include <stdio.h>int main() { int isProgrammingFun = 1; // Represents True int isFishTasty = 0; // Represents False
if (isProgrammingFun) { printf("Programming is awesome!\n"); }
if (!isFishTasty) { printf("I don't like fish.\n"); }
return 0; }
While this works perfectly fine, it isn't always obvious to a new developer reading the code that isProgrammingFun is intended to be a true/false flag rather than a regular number.
<stdbool.h>To make the language cleaner and more aligned with modern programming languages like C++ and Java, the C99 standard introduced the <stdbool.h> header file.
By importing this header, you gain access to the bool data type, as well as the true and false keywords. This drastically improves code readability!
#include <stdbool.h>bool variableName = true;
#include <stdio.h> #include <stdbool.h> // Required for bool, true, and falseint main() { bool isRaining = false; bool hasUmbrella = true;
if (isRaining && !hasUmbrella) { printf("You will get wet!\n"); } else if (isRaining && hasUmbrella) { printf("You are safe from the rain.\n"); } else { printf("It's a sunny day!\n"); }
// Printing boolean values // true prints as 1, false prints as 0 printf("isRaining value: %d\n", isRaining); printf("hasUmbrella value: %d\n", hasUmbrella);
return 0; }
Note: Even when using
<stdbool.h>, underneath the hood, C still storestrueas1andfalseas0. Therefore, when you useprintf()with the%dformat specifier, it will output1or0, not the words "true" or "false".
Booleans are most commonly generated as the result of a comparison operator (like ==, >, <). Whenever you compare two values, C evaluates the expression and returns a boolean equivalent (1 or 0).
#include <stdio.h> #include <stdbool.h>int main() { int myAge = 25; int votingAge = 18;
// Evaluates if myAge is greater than or equal to votingAge bool canVote = (myAge >= votingAge);
if (canVote) { printf("Eligible to vote!\n"); } else { printf("Not eligible to vote.\n"); }
return 0; }
To write clean, SEO-friendly, and maintainable C code, keep these rules in mind:
is, has, or can (e.g., isLoggedIn, hasError, canExecute). This makes your code read like plain English.<stdbool.h>: Always prefer including <stdbool.h> and using bool, true, and false instead of raw 1 and 0 when dealing with logic flags.if (isGameOver == true), you can simply write if (isGameOver). The == true part is redundant and considered an amateur practice.Q: Can I print "true" or "false" directly using printf?
A: No, C does not have a built-in format specifier to print boolean words. The %d specifier will print 1 or 0. If you want to print the words, you must use a conditional statement like:printf("%s\n", isTrue ? "true" : "false");
Q: What happens if I assign a negative number to a bool?
A: In C, any non-zero value evaluates to true. Therefore, if you assign -5 to a boolean variable, it will be evaluated and stored as 1 (true).
Q: Why was bool not included in the original C language?
A: The original C language was designed to be as minimal and efficient as possible. Since integers could perfectly handle logical zero and non-zero logic without requiring a separate type, the designers omitted it. It was added later in C99 for better readability and standard alignment.
Which header file must be included in C99 to use the bool keyword and true/false values?