In programming, you often need a data type that can only have one of two values: **YES / NO**, **ON / OFF**, or **TRUE / FALSE**. In C, these are called **Booleans**. While original C did not have a dedicated boolean type, modern C (C99 and later) provides a standard way to handle these values using the stdbool.h library.
Before the C99 standard, programmers used integers to represent booleans, where 0 represented False and any non-zero value (usually 1) represented True. This logic still works in C today, but using the actual boolean type makes code much cleaner.
To use the keywords bool, true, and false, you must include the <stdbool.h> header file at the top of your program. This is the professional way to handle logic in C.
Most of the time, you don't declare boolean variables manually. Instead, you create **Boolean Expressions** by comparing two values. These expressions are the brain of your program's decision-making process.
> : Greater than< : Less than== : Equal to>= : Greater than or equal toBooleans are most useful when combined with conditional statements like if...else. Here is a practical example to check if a person is old enough to vote:
Even though a boolean only needs 1 "bit" to store a 0 or 1, computers usually allocate **1 byte** (8 bits) of memory for a bool variable. This is because 1 byte is the smallest addressable unit of memory in most architectures. You can verify this using the sizeof() operator.
| Feature | Classic C Approach | Modern C (stdbool.h) |
|---|---|---|
| Data Type | int |
bool |
| Values | 0 and 1 | false and true |
| Readability | Low | High |
In high-performance systems and AI-driven applications developed today, boolean logic remains the foundation. Whether it's controlling a robotic arm or processing a search query, everything boils down to a series of true/false decisions made by the CPU.
printf, remember that there is no %b format specifier for booleans in standard C. You must use %d to print the integer representation (1 or 0).