About Lesson
Boolean:
In C, the boolean data type is not explicitly defined. However, it is common practice to use the _Bool data type or include the stdbool.h header to use the bool type for boolean values. In this example, I’ll demonstrate using the stdbool.h header.
Example:
#include <stdio.h>
#include <stdbool.h>int main() {
// Declaration and initialization of boolean variables
bool isSunny = true;// Using if-else statement with boolean condition
if (isSunny) {
printf(“It’s a sunny day!n”);
} else {
printf(“It’s not sunny today.n”);
}return 0;
}
Output:
It’s a sunny day!
Explanation of code:
- #include <stdio.h>: This line includes the standard input-output library for functions like printf.
- #include <stdbool.h>: This line includes the standard boolean library, which provides the bool type.
- int main(): The main function, where the program execution begins.
- bool isSunny = true;: Declaration and initialization of a boolean variable.
- if (isSunny) { … }: The if statement checks if isSunny is true.
- printf(“It’s a sunny day!n”);: If isSunny is true, this message is printed.
- else { … }: If isSunny is false, the code inside the else block is executed.
- printf(“It’s not sunny today.n”);: If isSunny is false, this message is printed.
- return 0;: Indicates successful program execution.