C Programming Tutorial 28 – The bool Data Type

In the previous video we learned how to use the _Bool data type. I recommend you read that blog before you read this one as it has fundamental knowledge that will help you here. Read it here! Are you new here? Start at the beginning, Intro to C!

If you’ve noticed, it seems that all the data types in C are lower case. We have int, double, float, char, but this _Bool had to go ruin everything for us.

Well, there is a way we can actually make this just bool. You first have to include a header file:

Now, we can declare a variable like this

Not only does this allow us to use the bool data type, but to make our lives easer we can use the words true or false rather than 0 or 1.

Yup, I exercised since the last video.

One important thing to note here is that the word false is not in quotes. We are not giving the variable a string with the word false in it. We are giving the value false with no quotes.

The way these work is no different than _Bool, it just makes it easier to work with. In fact, the values stored are still zero or one. Consider this example:

This will print the value 11 because the value of calebIsFat is 1.

Now, there is not a format character to print true or false, so for now the easiest way to print this is to use the same thing in the previous video, where we just print it as an integer. This will still work where 1 = true and 0 = false.

Later, when we get into something called conditionals, we could write a program that would say Caleb is fat or Caleb is not fat depending on the value of this variable, but for now, this is good.

Are you ready to learn more? Check out the next blog on variables now!

Leave a Reply

Your email address will not be published. Required fields are marked *