Before C99, C had no built-in bool keyword in everyday code—programmers used int with 0 and 1. <stdbool.h> (C99) adds bool, true, and false so conditions read clearly and match other languages.
01
bool
Type name.
02
true
Macro 1.
03
false
Macro 0.
04
_Bool
Under hood.
05
C99
Standard.
06
return
Functions.
Fundamentals
Definition and Usage
bool is a macro that expands to _Bool, a type that holds only two values: 0 (false) and 1 (true). Assigning any non-zero integer to a _Bool stores 1.
true and false are macros for integer constants 1 and 0. Use them for initialization and return values so intent is obvious. In if conditions, write if (flag) rather than if (flag == true).
💡
Beginner Tip
Include <stdbool.h> in every file that uses bool. The header also defines __bool_true_false_are_defined so headers can detect that true/false are available.
bool arrays work like other small integer types. Each element is 0 or 1. For many flags, a bit-field or unsigned int mask can save memory—bool is clearest for beginners.
Example 5 — 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
_Bool normalizes values: only 0 is false, everything else becomes 1. That matches how if (x) treats integers in C.
Loop conditions — while (running) with bool running.
Search results — return true when an item is found.
Config options — enable/disable features in settings structs.
Readability — clearer than magic int 0/1 in modern C99+ code.
🧠 How stdbool.h Works
1
Include header
bool, true, false macros become available.
Setup
2
Store 0 or 1
_Bool variables hold normalized true/false.
Storage
3
Use in conditions
if (flag) branches on zero vs non-zero.
Branch
=
💬
Readable logic
Code expresses yes/no intent without custom typedefs.
Important
📝 Notes
C99 feature—use -std=c99, c11, or newer.
bool promotes to int in expressions like other small integers.
sizeof(bool) is typically 1 byte but is implementation-defined.
Do not redefine true/false in your own headers.
printf("%d", flag) prints 0 or 1; there is no %b in standard C.
In C++, bool is a keyword—no need for stdbool.h in C++ files.
Performance
⚡ Optimization
bool is as cheap as a small integer. For huge arrays of flags, bit-packing into unsigned int masks saves memory but adds complexity. For clarity and typical programs, bool is fine.
Wrap Up
Conclusion
<stdbool.h> brings readable boolean types to C99+. Use bool for variables and return types, true and false for literals, and if (flag) for tests.
Remember normalization: only 0 is false. Include the header in every translation unit that uses these names.
Mix C stdbool.h blindly into C++ translation units
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about stdbool.h
Booleans in C, explained simply.
5
Core concepts
💬01
bool
_Bool macro.
Type
📚02
true
Is 1.
Macro
📈03
false
Is 0.
Macro
📄04
C99
Need -std.
Standard
🌐05
if (b)
Idiom.
Style
❓ 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. The old reference suggested flag == true for clarity—that is uncommon in real C code and can confuse readers.
You can, but _Bool stores only 0 or 1. Any non-zero value becomes 1 (true). When reading the variable later you get 0 or 1, not the original 5.
Not portably for bool/true/false names. You could use _Bool with literal 1/0, or int before C99. Always #include <stdbool.h> when you want the standard bool, true, and false macros.
Did you know?
C99 added _Bool to the language and put the friendly names in stdbool.h so existing code using macros named bool would not break. That is why the keyword is _Bool but everyday code says bool after including the header.