<stdbool.h> is a C99 header that defines bool, true, and false for readable yes/no values. Under the hood, bool is _Bool, and values normalize to 0 or 1.
Remember
#include <stdbool.h>
bool flag = true; // or false
if (flag) { ... } // idiomatic — not flag == true
bool is_even(int n) { return (n % 2) == 0; }
true → 1
false → 0
any non-zero assign → stored as 1
Use
Prefer
Yes/no flags and returns
bool / true / false
Built-in type name
_Bool (usually via bool)
Pre-C99 style
int with 0 / 1
Approach
How to Use stdbool.h in C
Include the header, declare bool variables or returns, and test with if (flag).
Add #include <stdbool.h> (compile with -std=c99 or -std=c11).
Declare bool variables or make functions return bool.
Assign true / false, or a comparison like (n % 2) == 0.
Test with if (flag) or if (!flag)—not == true.
Piece
Required?
Role
#include <stdbool.h>
Yes
Defines bool, true, false
bool
Yes
Type for 0/1 values (_Bool)
true / false
Common
Macros for 1 and 0
Pseudocode
Pseudocode
include stdbool.h
function is_even(n):
return (n % 2) == 0 // true or false
flag = false
flag = true
flag = !flag // toggle
if flag:
// do work
else:
// other path
Reference
Cheat Sheet
Keep these facts handy while writing boolean code.
Topic
Detail
Header
#include <stdbool.h> (C99+)
Type
bool → _Bool
Constants
true = 1, false = 0
Test
if (flag) / if (!flag)
Toggle
flag = !flag;
Normalize
Any non-zero assignment → stored as 1
Sample check
is_even(4) → true; is_even(7) → false
Try it
Live Preview
Simulate common bool operations: even check, toggle, and non-zero normalization.
Browser simulation of bool behavior (same idea as the C examples below).
Result
is_even(4)
→ (4 % 2) == 0
→ true (1)
4 is even.
Trace
Worked Walkthrough — is_even(4)
Trace a boolean helper: compare, store as bool, then branch with if.
Print friendly text. The ternary chooses "on" / "off" from the boolean.
Example 3 — Non-Zero Becomes true (Normalization)
Assigning 42 to a bool stores 1, not 42.
C
#include <stdbool.h>
#include <stdio.h>
int main(void) {
bool flag;
flag = 0;
printf("assign 0 -> flag = %d\n", flag);
flag = 42;
printf("assign 42 -> flag = %d\n", flag);
flag = -1;
printf("assign -1 -> flag = %d\n", flag);
return 0;
}
Output
assign 0 -> flag = 0
assign 42 -> flag = 1
assign -1 -> flag = 1
How It Works
Zero stays false.flag = 0 stores 0.
Any non-zero becomes 1.42 and -1 both store as true.
Original value is lost. You cannot recover 42 from the bool later.
Prefer true/false. Assign true/false (or comparisons) instead of magic integers.
Edge Cases & Pitfalls
Check these before relying on bool in real projects.
style
Avoid == true
Write if (flag) and if (!flag). Comparing to true is redundant in idiomatic C.
normalize
Non-zero collapses to 1
Do not store counters or error codes in a bool—only yes/no information survives.
standard
Needs C99+
Compile with -std=c99 or -std=c11. C89 code used int flags instead.
include
Include the header
Without <stdbool.h>, the names bool, true, and false are not portable.
Analysis
Performance Notes
Approach
Cost
Best for
bool / _Bool
Tiny (usually 1 byte)
Flags and return values
int 0/1
Often 4 bytes
Legacy C89 code
Bit masks
Packed bits
Many flags in tight structs
Prefer bool for clarity. Switch to bit fields or masks only when packing many flags matters for size or ABI.
Remember
Key Takeaways
Role: readable yes/no type via bool, true, false.
Storage: always 0 or 1 after assignment.
Style:if (flag), not if (flag == true).
Standard: C99+ with #include <stdbool.h>.
One line:stdbool.h gives C a clear boolean vocabulary—still just 0 and 1 under the hood.
Frequently Asked Questions
stdbool.h is a C99 header that defines bool as a macro for _Bool, plus true and false as macros for 1 and 0. It gives C a readable boolean type for conditions, flags, and function return values.
_Bool is the built-in keyword type (C99). bool is a macro in stdbool.h that expands to _Bool. In portable code, include stdbool.h and write bool—not _Bool directly—unless you have a special reason.
Yes. stdbool.h was added in C99. Compile with gcc -std=c99 or -std=c11. Older C89 compilers do not provide it; those programs used int with 0 and 1 instead.
Usually no. Idiomatic C uses if (flag) and if (!flag). Comparing to true is redundant and uncommon in real C code.
You can, but _Bool stores only 0 or 1. Any non-zero value becomes 1 (true). When you read the variable later you get 0 or 1, not the original 5.
Not portably for the names bool, true, and false. You could use _Bool with literal 1/0, or int before C99. Always #include <stdbool.h> when you want the standard macros.