Question
C does not appear to have a traditional built-in Boolean type like some other languages. What is the best way to represent and use Boolean values in C?
For example, what should be used for values that are logically true or false, and what is the recommended modern approach in C code?
Short Answer
By the end of this page, you will understand how Boolean values are represented in C, the difference between older and modern approaches, how to use _Bool and stdbool.h, and how to write clear, idiomatic C code when working with true/false conditions.
Concept
In C, Boolean logic has existed from the beginning, but for a long time the language did not provide a friendly Boolean type name like bool. Instead, C used integer values in conditions:
0means false- any non-zero value means true
That means code like this has always worked:
if (5) {
printf("true\n");
}
However, modern C provides a real Boolean type through:
- the built-in type
_Bool - the header
stdbool.h, which definesbool,true, andfalse
Example:
#include <stdbool.h>
bool is_ready = true;
This is the recommended approach in modern C because it makes code easier to read and communicates intent clearly.
Why this matters
Using a proper Boolean type improves:
- readability:
bool is_adminis clearer thanint is_admin - maintainability: future developers immediately understand the variable's purpose
- correctness of intent: the code expresses logical state, not arbitrary numbers
Important detail about _Bool
_Bool stores Boolean values as either 0 or 1.
If you assign any non-zero number to a _Bool, it becomes 1.
_Bool a = 0; // false
_Bool b = 7; // becomes 1
This makes _Bool useful when you want true/false behavior rather than general numeric storage.
Mental Model
Think of a Boolean as a light switch:
false= offtrue= on
In older C code, programmers often used an int as the switch:
0= off- any other number = on
That works, but it is like using a whole dimmer panel just to represent simple on/off state.
Using bool is like installing a real switch labeled ON/OFF. It makes the purpose obvious and avoids confusion.
Syntax and Examples
Modern C syntax
Use stdbool.h when available:
#include <stdbool.h>
int main(void) {
bool is_logged_in = true;
bool has_permission = false;
if (is_logged_in) {
printf("User is logged in\n");
}
if (!has_permission) {
printf("Access denied\n");
}
return 0;
}
What this does
boolis the Boolean typetrueandfalseare readable Boolean constants!has_permissionmeans "not has_permission"
Using _Bool directly
#include <stdio.h>
int main(void) {
_Bool ok = 1;
_Bool failed = 0;
printf("ok = %d\n", ok);
printf("failed = %d\n", failed);
return 0;
}
This works, but bool is usually easier to read.
Older style using int
Before stdbool.h, many programs used int:
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int main(void) {
int done = FALSE;
done = TRUE;
if (done) {
printf("Done\n");
}
return 0;
}
This still works, especially in older codebases, but modern code should prefer bool when possible.
Assignment behavior
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool a = 0;
bool b = 42;
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}
Output:
a = 0
b = 1
Even though 42 was assigned, bool stores it as 1.
Step by Step Execution
Consider this program:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool has_items = 3;
if (has_items) {
printf("Cart is not empty\n");
} else {
printf("Cart is empty\n");
}
return 0;
}
Step by step
#include <stdbool.h>makesbool,true, andfalseavailable.bool has_items = 3;assigns3to a Boolean.- Because
has_itemsis a Boolean type, the stored value becomes1. - The
if (has_items)condition checks whether the value is true. - Since
has_itemsis1, the condition is true. - The program prints:
Cart is not empty
Key idea
When using bool or _Bool:
0stays false- any non-zero value becomes true, stored as
1
Real World Use Cases
Boolean values are used everywhere in real programs.
Common use cases
-
Feature flags
bool dark_mode_enabledbool logging_enabled
-
Validation results
bool is_valid_emailbool input_ok
-
State tracking
bool is_connectedbool file_openbool is_finished
-
Permission checks
bool is_adminbool can_delete
-
Function return values
- a function can return
truefor success andfalsefor failure in simple cases
- a function can return
Example:
#include <stdbool.h>
bool is_even(int n) {
return n % 2 == 0;
}
This is common in utility functions and validation logic.
Real Codebase Usage
In real C codebases, developers usually use Boolean values in a few common patterns.
Guard clauses
Return early when a condition is not met:
#include <stdbool.h>
bool process_order(int order_id) {
if (order_id <= 0) {
return false;
}
return true;
}
Validation helpers
Small helper functions often return bool:
#include <stdbool.h>
#include <ctype.h>
bool is_digit_char(char c) {
return c >= '0' && c <= '9';
}
Configuration flags
Applications often store options as Booleans:
typedef struct {
bool verbose;
bool dry_run;
} Config;
Error handling
Some functions return bool to indicate success, while output data is written through pointers:
#include <stdbool.h>
bool divide(int a, int b, int *result) {
if (b == 0) {
return false;
}
*result = a / b;
return true;
}
This pattern is very common in C APIs.
Clear naming
Real projects often name Boolean variables with prefixes like:
is_has_can_should_
Examples:
is_readyhas_errorcan_retryshould_log
These names make conditions read like plain language.
Common Mistakes
1. Using int when bool would be clearer
This works:
int is_valid = 1;
But this is clearer:
bool is_valid = true;
Use bool when the value is truly just yes/no.
2. Forgetting to include stdbool.h
Broken code:
bool ready = true;
If stdbool.h is not included, bool, true, and false may be unknown.
Correct version:
#include <stdbool.h>
bool ready = true;
3. Comparing Boolean values unnecessarily
Less clear:
if (is_ready == true) {
Better:
if (is_ready) {
And instead of:
if (is_ready == false) {
Use:
if (!is_ready) {
4. Assuming only 1 is true in normal integer conditions
In C conditions, any non-zero value is true.
if (5) {
printf("true\n");
}
That is valid.
5. Mixing Boolean meaning with numeric meaning
Broken design:
int status = 2;
if (status) {
printf("Success? Maybe?\n");
}
Here status may represent multiple states, not just true/false. A bool would be wrong if more than two states are needed.
Use:
boolfor yes/noenumorintfor multiple statuses
6. Printing Booleans as words without handling formatting
printf does not have a special Boolean format specifier.
printf("%d\n", flag);
This prints 0 or 1.
If you want words:
printf("%s\n", flag ? "true" : "false");
Comparisons
Boolean options in C
| Approach | Example | Good for | Drawbacks |
|---|---|---|---|
_Bool | _Bool ready = 1; | Built-in Boolean storage | Less readable than bool |
bool from stdbool.h | bool ready = true; | Modern, readable C code | Requires including stdbool.h |
int with 0/non-zero | int ready = 1; | Legacy code, older C style | Less clear intent |
#define TRUE 1 and FALSE 0 | #define TRUE 1 | Older codebases | Can be inconsistent and less standard |
bool vs int
| Feature | bool | int |
|---|---|---|
| Represents logical true/false clearly | Yes | Not always |
| Stores arbitrary numbers | No | Yes |
| Best for conditions | Yes | Sometimes |
| Common in modern C | Yes | Mostly legacy or special cases |
When to use each
- Use
boolfor flags and yes/no states. - Use
intfor counts, arithmetic, and numeric status codes. - Use
enumwhen there are more than two possible states.
Cheat Sheet
Quick reference
Recommended modern approach
#include <stdbool.h>
bool flag = true;
flag = false;
Built-in Boolean type
_Bool flag = 1;
Boolean rules in C
0means false- non-zero means true in conditions
- assigning non-zero to
_Boolorboolstores1
Typical condition style
if (flag) {
/* true */
}
if (!flag) {
/* false */
}
Return a Boolean from a function
#include <stdbool.h>
bool is_positive(int n) {
return n > 0;
}
Print a Boolean
printf("%d\n", flag);
printf("%s\n", flag ? "true" : "false");
Best practice
- Prefer
booloverintfor logical values - Include
stdbool.h - Use clear names like
is_open,has_error,can_save - Do not use
boolfor values with more than two states
FAQ
Does C have a Boolean type?
Yes, modern C has the built-in type _Bool, and stdbool.h provides the friendlier alias bool plus true and false.
Should I use bool or int in C?
Use bool for values that are only true/false. Use int when the variable stores numeric data.
What header do I need for bool in C?
Include:
#include <stdbool.h>
Is true always equal to 1 in C?
With bool and _Bool, true is stored as 1. In general C conditions, any non-zero value is treated as true.
Can I use TRUE and FALSE macros?
You can, especially in older code, but modern C usually prefers bool, true, and false from stdbool.h.
Why does if (5) count as true in C?
Because C treats any non-zero value in a condition as true.
Can a function return bool in C?
Yes. This is common for validation, success/failure checks, and helper functions.
What should I use if there are more than two states?
Use an enum or another numeric type instead of bool.
Mini Project
Description
Build a small program that checks whether a user is allowed to access a feature. This project demonstrates how to use bool for readable decision-making in C, including validation and conditional output.
Goal
Create a C program that uses Boolean variables and Boolean-returning functions to decide whether access should be granted.
Requirements
- Include and use
stdbool.h - Create at least one function that returns a
bool - Store at least two yes/no states using Boolean variables
- Print whether access is granted or denied based on the conditions
Keep learning
Related questions
2D Array Loop Order and Cache Performance in C
Learn why swapping nested loops changes 2D array performance in C, using row-major memory layout, cache locality, and practical benchmarks.
Array-to-Pointer Conversion in C and C++ Explained
Learn what array-to-pointer conversion means in C and C++, how array decay works, and how it differs from a pointer to an array.
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.