Booleans with stdbool.h in C
Original C had no boolean type — conditions used ints with 0 false and nonzero true, a convention baked into every API. C99 added _Bool plus <stdbool.h> providing bool, true, and false macros; C23 finally made bool, true, and false real keywords.
The old convention still rules interop: functions return 0/-1 or pointers (NULL means false), and any nonzero integer tests true. Use bool for new logic (self-documenting conditions), but read legacy int-flag code fluently — it is everywhere.
Zero is false, nonzero true
if (n), while (*p), for (;x;) — C tests integers, pointers, and chars directly; only exact zero/NULL fails.
stdbool.h for new code
#include <stdbool.h> gives bool/true/false on C99–C17 (keywords since C23) — write conditions that read as logic.
Legacy int flags
Old code returns int success codes and tests pointers for NULL — recognize the idiom instead of fighting it.
Print with %d
bool prints as 1/0 with %d — there is no %bool, then or now.
#include <stdio.h>#include <stdbool.h> bool is_even(int n) { return n % 2 == 0; } int main(void) { printf("%d %d\n", is_even(4), is_even(5)); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.