Booleans in C: An Introduction
Learn how to use booleans in C and discover the native support introduced in C99.
C, originally, did not have built-in boolean values. However, with the release of C99 in 1999/2000, a boolean type was introduced. Although it requires importing a header file, it is commonly referred to as a “native” boolean type. Let’s explore how to use it.
To begin, include the necessary header files:
1 |
Now, you can use the bool
type as follows:
1 | int main(void) { |
Note that if you are programming for Arduino, you can simply use the bool
type without including stdbool.h
because it is a valid and built-in C++ data type. The Arduino Language is based on C++.
However, in standard C, make sure to include #include <stdbool.h>
to avoid errors when declaring and using bool
variables. Otherwise, you may encounter error messages like the following:
1 | ➜ ~ gcc hello.c -o hello; ./hello |
By utilizing the boolean type in C, you can better express conditions and make your code more readable.
Tags: C boolean, C99, boolean type, header file, bool variable