C stdbool.h Library Reference
Complete reference for stdbool.h - bool type, true/false values, and boolean best practices in C99+.
Track Your Progress
Sign in to save your learning progress
What You Will Learn
- ✓Use bool, true, and false in C
- ✓Understand truthiness in C
- ✓Write functions returning boolean
- ✓Follow boolean best practices
?Why Use stdbool.h?
Before C99, C had no bool type! Programmers used int with 0/1 or created their own #define TRUE 1. This was error-prone and inconsistent.
Before C99
#define FALSE 0
int is_valid = TRUE;
✓With stdbool.h
bool is_valid = true;
C23 Update: In C23, bool, true, and false are built-in keywords — no header needed!
01Introduction to stdbool.h
✓ What is stdbool.h?
<stdbool.h> was introduced in C99 to provide a standard Boolean type for C. Before C99, programmers had to define their own boolean types.
It defines the type bool and the values true and false.
What stdbool.h Provides
| Name | Definition | Description |
|---|---|---|
| bool | _Bool | Boolean type (0 or 1) |
| true | 1 | Boolean true value |
| false | 0 | Boolean false value |
| __bool_true_false_are_defined | 1 | Macro to check if bool is defined |
02Before C99 (The Old Way)
Before C99, programmers had to define their own boolean types:
1// Old way 1: Using #define2#define TRUE 13#define FALSE 045// Old way 2: Using typedef with enum6typedef enum { FALSE, TRUE } Boolean;78// Old way 3: Using typedef with int9typedef int BOOL;10#define TRUE 111#define FALSE 01213// Usage (old way)14int is_valid = TRUE;15if (is_valid) {16 printf("Valid!\n");17}Problems with Old Approach:
- No standard - different codebases used different names
- No type safety - could assign any integer value
- Name conflicts possible (TRUE, FALSE, BOOL)
03Using stdbool.h (The Modern Way)
1#include <stdio.h>2#include <stdbool.h>34int main() {5 // Declare boolean variables6 bool is_running = true;7 bool is_paused = false;8 9 printf("is_running: %d\n", is_running); // 110 printf("is_paused: %d\n", is_paused); // 011 12 // Boolean in conditions13 if (is_running) {14 printf("The program is running\n");15 }16 17 if (!is_paused) {18 printf("The program is not paused\n");19 }20 21 // Toggle boolean22 is_running = !is_running;23 printf("After toggle, is_running: %d\n", is_running); // 024 25 return 0;26}2728/* Output:29is_running: 130is_paused: 031The program is running32The program is not paused33After toggle, is_running: 034*/04Boolean Expressions
Comparison operators and logical operators return boolean values:
1#include <stdio.h>2#include <stdbool.h>34int main() {5 int a = 10, b = 20;6 7 // Comparison operators return bool8 bool is_equal = (a == b); // false9 bool is_less = (a < b); // true10 bool is_greater = (a > b); // false11 12 printf("a == b: %d\n", is_equal);13 printf("a < b: %d\n", is_less);14 printf("a > b: %d\n", is_greater);15 16 // Logical operators17 bool both_positive = (a > 0) && (b > 0); // true18 bool either_zero = (a == 0) || (b == 0); // false19 bool not_equal = !(a == b); // true20 21 printf("\nBoth positive: %d\n", both_positive);22 printf("Either zero: %d\n", either_zero);23 printf("Not equal: %d\n", not_equal);24 25 return 0;26}2728/* Output:29a == b: 030a < b: 131a > b: 03233Both positive: 134Either zero: 035Not equal: 136*/05Functions Returning Boolean
1#include <stdio.h>2#include <stdbool.h>3#include <ctype.h>45// Function returning bool6bool is_even(int n) {7 return n % 2 == 0;8}910bool is_prime(int n) {11 if (n <= 1) return false;12 if (n <= 3) return true;13 if (n % 2 == 0 || n % 3 == 0) return false;14 15 for (int i = 5; i * i <= n; i += 6) {16 if (n % i == 0 || n % (i + 2) == 0) {17 return false;18 }19 }20 return true;21}2223bool is_valid_age(int age) {24 return age >= 0 && age <= 150;25}2627bool is_vowel(char c) {28 c = tolower(c);29 return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';30}3132int main() {33 printf("is_even(10): %s\n", is_even(10) ? "true" : "false");34 printf("is_even(7): %s\n", is_even(7) ? "true" : "false");35 36 printf("\nis_prime(17): %s\n", is_prime(17) ? "true" : "false");37 printf("is_prime(20): %s\n", is_prime(20) ? "true" : "false");38 39 printf("\nis_valid_age(25): %s\n", is_valid_age(25) ? "true" : "false");40 printf("is_valid_age(-5): %s\n", is_valid_age(-5) ? "true" : "false");41 42 printf("\nis_vowel('A'): %s\n", is_vowel('A') ? "true" : "false");43 printf("is_vowel('X'): %s\n", is_vowel('X') ? "true" : "false");44 45 return 0;46}4748/* Output:49is_even(10): true50is_even(7): false5152is_prime(17): true53is_prime(20): false5455is_valid_age(25): true56is_valid_age(-5): false5758is_vowel('A'): true59is_vowel('X'): false60*/06Boolean Arrays
1#include <stdio.h>2#include <stdbool.h>34#define SIZE 1056int main() {7 // Boolean array (useful for flags, visited markers, etc.)8 bool visited[SIZE] = {false}; // All initialized to false9 10 // Mark some as visited11 visited[0] = true;12 visited[3] = true;13 visited[7] = true;14 15 // Print status16 printf("Visited status:\n");17 for (int i = 0; i < SIZE; i++) {18 printf("Index %d: %s\n", i, visited[i] ? "visited" : "not visited");19 }20 21 // Count visited22 int count = 0;23 for (int i = 0; i < SIZE; i++) {24 if (visited[i]) count++;25 }26 printf("\nTotal visited: %d\n", count);27 28 // Sieve of Eratosthenes example29 bool is_prime[20] = {0};30 for (int i = 2; i < 20; i++) is_prime[i] = true;31 32 for (int i = 2; i * i < 20; i++) {33 if (is_prime[i]) {34 for (int j = i * i; j < 20; j += i) {35 is_prime[j] = false;36 }37 }38 }39 40 printf("\nPrimes under 20: ");41 for (int i = 2; i < 20; i++) {42 if (is_prime[i]) printf("%d ", i);43 }44 printf("\n");45 46 return 0;47}4849/* Output:50Visited status:51Index 0: visited52Index 1: not visited53Index 2: not visited54Index 3: visited55...56Total visited: 35758Primes under 20: 2 3 5 7 11 13 17 1959*/07Boolean in Structures
1#include <stdio.h>2#include <stdbool.h>3#include <string.h>45typedef struct {6 char name[50];7 int age;8 bool is_active;9 bool is_admin;10 bool is_verified;11} User;1213void print_user(User *u) {14 printf("Name: %s\n", u->name);15 printf("Age: %d\n", u->age);16 printf("Active: %s\n", u->is_active ? "Yes" : "No");17 printf("Admin: %s\n", u->is_admin ? "Yes" : "No");18 printf("Verified: %s\n", u->is_verified ? "Yes" : "No");19 printf("---\n");20}2122int main() {23 User users[3] = {24 {"Alice", 25, true, false, true},25 {"Bob", 30, true, true, true},26 {"Charlie", 22, false, false, false}27 };28 29 // Print all users30 for (int i = 0; i < 3; i++) {31 print_user(&users[i]);32 }33 34 // Count active users35 int active_count = 0;36 for (int i = 0; i < 3; i++) {37 if (users[i].is_active) active_count++;38 }39 printf("Active users: %d\n", active_count);40 41 // Find admins42 printf("\nAdmins: ");43 for (int i = 0; i < 3; i++) {44 if (users[i].is_admin) {45 printf("%s ", users[i].name);46 }47 }48 printf("\n");49 50 return 0;51}08Truthiness in C
Important Rule:
In C, 0 is false, and any non-zero value is true. This applies to integers, pointers, and any expression used in a boolean context.
1#include <stdio.h>2#include <stdbool.h>34int main() {5 // Different values in boolean context6 int zero = 0;7 int positive = 42;8 int negative = -1;9 int *null_ptr = NULL;10 int value = 100;11 int *valid_ptr = &value;12 13 printf("Truthiness of different values:\n");14 printf("0: %s\n", zero ? "true" : "false");15 printf("42: %s\n", positive ? "true" : "false");16 printf("-1: %s\n", negative ? "true" : "false");17 printf("NULL ptr: %s\n", null_ptr ? "true" : "false");18 printf("Valid ptr: %s\n", valid_ptr ? "true" : "false");19 20 // Assignment to bool normalizes to 0 or 121 bool b1 = 42; // Becomes 122 bool b2 = -100; // Becomes 123 bool b3 = 0; // Becomes 024 25 printf("\nNormalization to bool:\n");26 printf("bool b1 = 42: %d\n", b1);27 printf("bool b2 = -100: %d\n", b2);28 printf("bool b3 = 0: %d\n", b3);29 30 return 0;31}3233/* Output:34Truthiness of different values:350: false3642: true37-1: true38NULL ptr: false39Valid ptr: true4041Normalization to bool:42bool b1 = 42: 143bool b2 = -100: 144bool b3 = 0: 045*/09Best Practices
✓ Do This
// Use descriptive names starting with is_, has_, can_, should_bool is_valid;bool has_permission;bool can_edit;bool should_retry;// Return bool from validation functionsbool is_valid_email(const char *email);// Use bool for flagsvoid process(bool verbose, bool dry_run);✗ Avoid This
// Don't compare booleans to true/falseif (is_valid == true) // Redundantif (is_valid) // ✓Betterif (flag == false) // Redundantif (!flag) // ✓Better// Don't use int for boolean valuesint is_ready = 1; // Use boolbool is_ready = true; // ✓Better10C23 Update
C23 Change:
In C23, bool, true, and false are built-in keywords. You no longer need to include <stdbool.h>. However, including it still works for backwards compatibility.
1// C23 and later - no header needed!2// bool, true, false are keywords34int main() {5 bool flag = true; // Works without #include <stdbool.h>6 return 0;7}89// For compatibility with older standards, you can still use:10#include <stdbool.h> // Still works, becomes no-op in C23!Code Pitfalls: Common Mistakes & What to Watch For
Copied code often uses bool/true/false without including stdbool.h or checking C standard compatibility.
If you search online to "write a function that returns true or false," it might simply use bool, true, and false without #include <stdbool.h>. This works in C++ but fails in C (before C23). The Copied code might also assume sizeof(bool) == 1, which isn't guaranteed.
The Trap: Online sources often blur C and C++ conventions. In C, bool is defined as a macro in stdbool.h (before C23). Any non-zero value is "truthy," but Copied code might write if (flag == true) which fails for values like 2 or -1 that are logically true but not equal to 1.
The Reality: Always include <stdbool.h> for C99-C17 code. Use if (flag) instead of if (flag == true). Be aware that bool is just an alias for _Bool, which is an unsigned integer type. For pre-C99 code, use int with 0/1.
12Frequently Asked Questions
Q:What's the difference between bool and _Bool?
A: _Bool is the actual type built into C99. bool is a macro that expands to _Bool. They're identical — use bool for readability.
Q:What format specifier should I use for bool?
A: Use %d — bool is an integer type. It prints 0 for false, 1 for true. For readable output, use a ternary: flag ? "true" : "false"
Q:Should I compare booleans to true/false?
A: No! Useif (flag) notif (flag == true). For negation, use if (!flag) notif (flag == false).
Q:Do I still need stdbool.h in C23?
A: No! In C23, bool, true, and false are keywords — no header needed. Including stdbool.h still works (for backwards compatibility) but does nothing in C23.
Q:Can I use bool in a struct?
A: Yes! But remember that bool typically takes 1 byte despite only needing 1 bit. For memory-critical applications with many boolean flags, consider using bit fields: struct Flags {unsigned int active : 1; unsigned int visible : 1;}
Q:What happens when I assign a negative number to bool?
A: Any non-zero value (including negative numbers) becomes true (1). So bool b = -5; results in b being 1 (true). This is because bool conversion follows C's truthiness rules: zero is false, everything else is true.
Q:How do I create an array of booleans?
A: Simply declare it like any other array:bool flags[10] = {false}; initializes all elements to false. Each element occupies 1 byte. For large boolean arrays where memory matters, consider using bit manipulation with unsigned integers instead.
Q:Why does sizeof(bool) return 1, not 0.125 (1 bit)?
A: C requires all addressable types to be at least 1 byte (8 bits). While bool only needs 1 bit logically, the smallest addressable unit in C is a byte. The compiler may use the extra 7 bits for padding or optimization purposes. This is why bit fields exist for packing multiple flags efficiently.
13Best Practices for Boolean Usage
Do This
- • Use descriptive names:
isValid,hasPermission - • Return bool from validation functions
- • Initialize bool variables explicitly
- • Use
if (flag)instead ofif (flag == true)
Avoid This
- • Using int for boolean values in new code
- • Comparing booleans to true/false directly
- • Assuming bool is exactly 1 bit in size
- • Mixing bool with bitwise operations carelessly
12Quick Reference
Types & Values
bool- Boolean type (alias for _Bool)true- Value 1false- Value 0
Truthiness
0→ false- Non-zero → true
NULL→ false
time.h Library
Next →limits.h Library
Test Your Knowledge
Related Tutorials
C limits.h Library Reference
Complete reference for limits.h - integer limits, overflow prevention, and portable code with INT_MAX, INT_MIN, etc.
C time.h Library Reference
Complete reference for time.h - getting time, formatting dates, measuring durations, and working with timestamps.
C ctype.h Library Reference
Complete reference for ctype.h - character classification (isalpha, isdigit) and conversion (toupper, tolower).
Have Feedback?
Found something missing or have ideas to improve this tutorial? Let us know on GitHub!