How to use bool in c?

There is no bool data type in C. In C99 standerd version, the boolean variable has been added as _Bool. Additionally, a new header stdbool.h has been added for compatibility reasons. This header allows programmers to use boolean types in the same way, as in C++ language.

To use bool in C, we can use enum as below.

1
2
3
4
5
6
7
8
9
10
11
12
enum bool {
    false, true
};
 
bool value;
value = bool(0); // False
value = bool(1); // True
 
if(value == false)
	printf("Value is false");
else
	printf("Value is true");

Here we can use variable ‘value’ as boolean variable.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in