- If you like puzzles, you're probably using Boolean types
without even knowing.
If you ever hit a keyboard,
if you ever do anything on a computer,
you are instructing the electrical circuits
inside the computer to switch a value from zero to one.
Anytime you switch a value from zero to one,
and those are the only type of value a variable can take,
this is called a Boolean variable.
The word Boolean comes from George Boole,
who is the inventor of Boolean algebra,
Boolean logic circuits, and so on.
As I said, they simply denote
a value of being true or false.
For something so tiny, the entire computer industry
is built on them, however, despite them being so important,
Boolean types are not defined in standard C,
but they're workarounds.
Like with everything in programming,
be prepared to find ways to do things
that otherwise are not either explicitly defined
or not possible at first glance.
One is true, which hints that if we ever want to use
the Boolean value true, we simply would use the number one.
Conversely, if we want to refer to false,
we can simply use the number zero.
In fact, the information in computers is stored as
ones and zeros, and everything we do as computers
is actually Boolean algebra.
Let's see how this works in code.
Notice a second or a third library called stdbool.
This is a standard Boolean library
that has been added to C99,
which is the previous version of C, before C11.
It is not part of the original C.
For the original C, you have to do something
like in line 20 and 21, where we simply use
the define processor directive
to say that true is one, and false is zero.
Notice on line 19 there is a new command called a typedef.
We are defining a new type, Bool.
We're not really defining a new type.
We're simply saying that Bool is an int.
This is mostly for clarity,
so that if you want to define a variable later on
and don't want to use int,
because you want to draw attention to the fact that
this is a different type of variable,
you can simply say typedef int Bool,
which means I'm redefining Bool to simply be int.
Once we did that, we can use,
on line 25, Bool aBooleanVariable.
This means that it's just an int,
but we'll be sure to assign it only values of zeros and one.
For example, we can say on line 26
a Boolean variable equals true, because on line 20
we defined true equals one,
and on line 25 we declared a Boolean variable
to be a type Bool, which is actually an int.
So, all we said that this is an integer
who is assigned the value one, but for clarity,
this is the workaround that makes us feel as if
we're working with explicitly restricted Boolean variables
that can only accept zero and one.
Let's print that by running the code and see what happens.
Not surprisingly, the value of a Boolean variable is one,
because we assigned one to it.
Once you get more comfortable with voids,
with Booleans, with the sizes of integers and float,
you will be able to make a very clear determination
about which type of variable is most suitable
to a given situation.
Notice that there are workarounds
that are not just computational but mostly semantics.
This should give you an idea of what's possible.
Try to think out of the box.
Maybe you can come up with your own types of variables,
maybe with your own words for them.
Maybe there are ways to define certain things
that will help see a problem in a different way
and make it easier to tackle.
All of this is possible,
and we will see in the next video how.