Boolean values in C language
C - Using boolean values in C
Option – 1:
#include <stdbool.h>
The header stdbool.h in the C Standard Library for the C programming language contains four macros for a Boolean data type. The macros as defined in the ISO C standard are :
- bool which expands to _Bool
- true which expands to 1
- false which expands to 0
- __bool_true_false_are_defined which expands to 1
Example: boolean values in C using stdbool.h
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void) {
bool color = true; // Could also be `bool color = 1;`
while(color) {
printf("This will run as long as color is true.\n");
color = false;
}
printf("color is false!\n");
return 0;
}
Output:
This will run as long as color is true. color is false!
Option – 2:
typedef enum { F, T } boolean;
Example: Using typedef enum { F, T } boolean;
#include <stdio.h>
typedef enum {
F, T
}
boolean;
int main(void) {
boolean color = T;
while(color) {
printf("This will run as long as color is true.\n");
color = F;
}
printf("color is false!\n");
return 0;
}
Output:
This will run as long as color is true. color is false!
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join