C Booleans
Programs constantly need yes/no answers: Is the file open? Is the user logged in? Is the number positive? A value that is either true or false is called a boolean.
Booleans in C: numbers in disguise
Original C had no special boolean type. Instead, it uses a simple rule that still applies everywhere today:
- 0 means false
- any non-zero value means true
Comparison and logical operators produce the int values 1 (true) or 0 (false):
#include <stdio.h>
int main(void) {
printf("10 > 3 -> %d\n", 10 > 3);
printf("10 == 3 -> %d\n", 10 == 3);
printf("!0 -> %d\n", !0);
printf("!42 -> %d\n", !42);
return 0;
}10 > 3 -> 1
10 == 3 -> 0
!0 -> 1
!42 -> 0
The bool type from <stdbool.h>
Since C99, you can include <stdbool.h> to get the names bool, true and false. This makes your intent obvious to anyone reading the code:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool is_raining = true;
bool has_umbrella = false;
printf("is_raining = %d\n", is_raining);
printf("has_umbrella = %d\n", has_umbrella);
printf("will get wet = %d\n", is_raining && !has_umbrella);
printf("sizeof(bool) = %zu\n", sizeof(bool));
return 0;
}is_raining = 1
has_umbrella = 0
will get wet = 1
sizeof(bool) = 1
Under the hood, bool is the built-in type _Bool, and true/false are just 1 and 0. There is no % specifier for booleans; print them with %d, or turn them into words yourself:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool door_locked = true;
printf("Door locked? %s\n", door_locked ? "yes" : "no");
door_locked = false;
printf("Door locked? %s\n", door_locked ? "yes" : "no");
return 0;
}Door locked? yes
Door locked? no
A bool variable can only hold 0 or 1. Any non-zero value assigned to it is stored as 1:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool flag = 250; // non-zero -> true (1)
int as_int = 250;
printf("bool: %d, int: %d\n", flag, as_int);
return 0;
}bool: 1, int: 250
Note: In the newest standard, C23,
bool,trueandfalseare built-in keywords and the header is no longer needed. In C17 (used here) you still include<stdbool.h>.
Boolean expressions in practice
Booleans really shine when you give conditions descriptive names. Instead of repeating a complicated expression, store it in a well-named variable:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int age = 17;
bool with_parent = true;
bool is_adult = age >= 18;
bool may_enter = is_adult || with_parent;
if (may_enter) {
printf("Welcome to the cinema!\n");
} else {
printf("Sorry, you cannot enter.\n");
}
return 0;
}Welcome to the cinema!
Truthiness of other values
Because any non-zero value counts as true, you will often see code that tests numbers or pointers directly:
| Expression | Means |
|---|---|
if (count) |
if (count != 0) |
if (!count) |
if (count == 0) |
if (ptr) |
if (ptr != NULL) |
while (*s) |
while the current character is not the terminating '\0' |
Tip: Comparing a boolean to
true(if (done == true)) is redundant and can even be wrong for plainintflags that hold values like 2. Just writeif (done).
Test yourself
Which values does C treat as false?
- Only the keyword false
- Zero
- Negative numbers
- Any number under 1
Which header provides bool, true and false in C17?
- <stdio.h>
- <stdlib.h>
- <stdbool.h>
- <bool.h>
What does printf("%d", 4 != 4); print?
- 1
- 0
- false
- 4