C Booleans
Booleans
Very often, in programming, you will need a data type that can only have one of two values, like:
- YES / NO
- ON / OFF
- TRUE / FALSE
For this, C has a bool
data type, which is
known as booleans.
Booleans represent values that are either true
or false
.
Boolean Variables
In C, the bool
type is not a built-in data type, like int
or char
.
It was introduced in C99, and you must import the following header file to use it:
#include <stdbool.h>
A boolean variable is declared with the bool
keyword and can only take the
values true
or false
:
bool isProgrammingFun = true;
bool isFishTasty = false;
Before trying to print the boolean variables, you should know that boolean values are returned as integers:
1
(or any other number that is not 0) representstrue
0
representsfalse
Therefore, you must use the %d
format specifier to print a boolean value:
Example
// Create boolean variables
bool isProgrammingFun = true;
bool isFishTasty = false;
//
Return boolean values
printf("%d",
isProgrammingFun); // Returns 1 (true)
printf("%d", isFishTasty);
// Returns 0 (false)
Try it Yourself »
However, it is more common to return a boolean value by comparing values and variables.
Comparing Values and Variables
Comparing values are useful in programming, because it helps us to find answers and make decisions.
For example, you can use a comparison operator,
such as the greater than (>
)
operator, to compare two values:
From the example above, you can see that the return value is a boolean value.
You can also compare two variables:
In the example below, we use the equal to (==
)
operator to compare different values:
Example
printf("%d", 10 == 10); // Returns 1 (true), because 10 is equal to 10
printf("%d", 10 == 15); // Returns 0 (false), because 10 is not equal
to 15
printf("%d", 5.5 == 55); // Returns 0 (false) because
5.5 is not equal to 55
printf("%d", 3.8 == 3.8); // Returns 1 (true)
because 3.8 is equal to 3.8
Try it Yourself »
You are not limited to only compare numbers. You can also compare boolean variables, or even special structures, like arrays (which you will learn more about in a later chapter):
Example
bool isHamburgerTasty = true;
bool isPizzaTasty = true;
// Find out
if both hamburger and pizza is tasty
printf("%d", isHamburgerTasty ==
isPizzaTasty);
Try it Yourself »
Remember to include the <stdbool.h>
header file when working with bool
variables.
Real Life Example
Let's think of a "real life example" where we need to find out if a person is old enough to vote.
In the example below, we use the >=
comparison operator to find out if the age (25
) is greater than OR
equal to the voting age limit, which is set
to 18
:
Example
int myAge = 25;
int votingAge = 18;
printf("%d", myAge >=
votingAge); // Returns 1 (true), meaning 25 year olds are allowed to vote!
Try it Yourself »
Cool, right? An even better approach (since we are on a roll now), would be to wrap the code above in an
if...else
statement, so we can perform
different actions depending on the result:
Example
Output "Old enough to vote!" if myAge
is greater than or equal to 18
. Otherwise output "Not old enough to vote.":
int myAge = 25;
int votingAge = 18;
if (myAge >= votingAge) {
printf("Old enough to vote!");
} else {
printf("Not old enough
to vote.");
}
Try it Yourself »
Booleans are the basis for all comparisons and conditions.
You will learn more about conditions (if...else
) in the next chapter.