Prerequisite: Bool Data Type in C++
The C99 standard for C language supports bool variables. Unlike C++, where no header file is needed to use bool, a header file “stdbool.h” must be included to use bool in C. If we save the below program as .c, it will not compile, but if we save it as .cpp, it will work fine.
If we include the header file “stdbool.h” in the above program, it will work fine as a C program.
There is one more way to do it using enum function in C language. You can create a bool using enum. One enum will be created as bool, then put the elements of enum as True and False respectively. The false will be at the first position, so it will hold 0, and true will be at the second position, so it will get value 1.
Below is the implementation of the above idea:
bool1 is false bool2 is true
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- gets() is risky to use!
- Do not use sizeof for array parameters
- Can we use function on left side of an expression in C and C++?
- What is use of %n in printf() ?
- Use of realloc()
- When do we use Initializer List in C++?
- Use of explicit keyword in C++
- How to use getline() in C++ when there are blank lines in input?
- Use of fflush(stdin) in C
- How to use POSIX semaphores in C language
- Use of & in scanf() but not in printf()
- Why strcpy and strncpy are not safe to use?
- Why to use fgets() over scanf() in C?
- Features and Use of Pointers in C/C++
- How can we use Comma operator in place of curly braces?
- How to use make utility to build C projects?`
Improved By : pulkitagarwal03pulkit