Booleans in C
New Courses Coming Soon
Join the waiting lists
An introduction to how to use booleans in C
C originally did not have native support for boolean values.
C99, the version of C released in 1999/2000, introduced a boolean type.
To use it, however, you need to import a header file, so I’m not sure we can technically call it “native”. Anyway, we do have a bool
type.
You can use it like this:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool isDone = true;
if (isDone) {
printf("done\n");
}
isDone = false;
if (!isDone) {
printf("not done\n");
}
}
If you’re programming the Arduino, you can use bool
without including stdbool
because bool
is a valid and built-in C++ data type, and the Arduino Language is C++.
In plain C, remember to #include <stdbool.h>
otherwise you’ll get a bunch of errors at declaration and any time you use the bool
variable:
➜ ~ gcc hello.c -o hello; ./hello
hello.c:4:3: error: use of undeclared identifier
'bool'
bool isDone = true;
^
hello.c:5:7: error: use of undeclared identifier
'isDone'
if (isDone) {
^
hello.c:8:8: error: use of undeclared identifier
'isDone'
if (!isDone) {
^
3 errors generated.
→ Get my C Handbook
Here is how can I help you:
- COURSES where I teach everything I know
- CODING BOOTCAMP cohort course - next edition in 2025
- THE VALLEY OF CODE your web development manual
- BOOKS 17 coding ebooks you can download for free on JS Python C PHP and lots more
- Interesting links collection
- Follow me on X