C Programming Tutorial 27 – _Bool Data Type – Part 1

Don’t forget to read the last blog on the ASCII and Int Conversion! Are you new here? Start at the beginning, Intro to C!

In this video we are going to be discussing Boolean data. Boolean data is anything that is either true or false.

Now, if you remember back a few videos ago when I started talking to you about binary, I mentioned how a single bit can be either 1 or 0. It works pretty similar with a Boolean variable.

To create a Boolean variable, we use _Bool (the B must be capitalized):

Notice the naming convention here. I have decided to lower case my name to follow what is known as camel-case naming convention. That is where the first character of the first word is lower case, but every word after that is upper case. Another common convention is to use all lower case with underscores to separate the words. Whatever you use is up to you:

Now, the reason I named this variable calebIsFat is not because he is, but rather that he might be. I’m not saying that Caleb is fat. I’m saying that the statement “Caleb is fat” is either true or false. This is the very foundation for what is known as Boolean logic, which you will find when you study discrete mathematics, computer engineering, computer science, and a ton of other cool stuff.

Now, we need to assign a value to this variable. This means we have to ask ourselves…Is Caleb fat? Well, I wouldn’t go so far as to say I’m fat, but I could use some trimming around the waist. In fact, I had to buy new dress clothes for my videos because I’m so fat. Therefore, I am going to go with yes.

To assign true to this variable, use 1 (which represents true). If you want to assign false, use 0. Actually though, if you store anything besides zero, it is stored as 1. This means that anything that is not zero is stored as true. So to express my fatness, I could do this:

Now my fat level is over 9000.

How do we output these values though? Well, you can output just as if it were an integer:

You can see that according to my program, indeed I am fat.

Now, this is cool and all, but some of this isn’t very intuitive. Like, why is it called _Bool? You also have to remember that 1 is true and 0 is false, and it’s just plain hard.

I have good news though! There is an easier way to represent Boolean data, and that is what I am going to talk about in the next blog! Check it out now!

Leave a Reply

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