C Booleans

C Booleans: True or False

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.


1. The Legacy Way: Using Integers

Before the C99 standard, C programmers exclusively used integers to represent boolean values.

Boolean via Integers Example

#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.


2. The Modern Way: <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!

Syntax

#include <stdbool.h>

bool variableName = true;

Using stdbool.h Example

#include <stdio.h>
#include <stdbool.h> // Required for bool, true, and false

int 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 stores true as 1 and false as 0. Therefore, when you use printf() with the %d format specifier, it will output 1 or 0, not the words "true" or "false".


3. Booleans in Expressions

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).

Boolean Expression Example

#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; }


4. Best Practices for Booleans

To write clean, SEO-friendly, and maintainable C code, keep these rules in mind:

  1. Use Descriptive Names: Name your boolean variables starting with verbs like is, has, or can (e.g., isLoggedIn, hasError, canExecute). This makes your code read like plain English.
  2. Use <stdbool.h>: Always prefer including <stdbool.h> and using bool, true, and false instead of raw 1 and 0 when dealing with logic flags.
  3. Avoid Redundant Checks: Instead of writing if (isGameOver == true), you can simply write if (isGameOver). The == true part is redundant and considered an amateur practice.

5. Frequently Asked Questions (FAQ)

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.


Exercise

?

Which header file must be included in C99 to use the bool keyword and true/false values?