top of page
Bool
Boolean variables are two state true and false variables.
We can use a number like a boolean variable in that we execute code if the value is equal to 1 and do not execute if the variable is not equal to 1. This is not a 'proper' boolean variable but will often get the job done.
Example:
Output:


Notice how simply using if(x) automatically uses our input from Scanf for the comparison.
To define an actual boolean variable in C we will have to add the stdbool.h library. Now we can define a bool using
bool name = true / false;
Example:
Output:


This essentially does the same thing as we did earlier but can be easier to read and manage.
bottom of page