Skip to content
Home » How to use Boolean values in C

How to use Boolean values in C

Learn about C Program to use Boolean Values in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

C Program to use Boolean Values:

Boolean values in c can be used by including standard library namely “stdbool.h” .This library helps to take any variable with boolean values. This is as simple as declaring a variable to integer ,we use int to declare it whereas for declaring boolean values we use keyword bool.

Method 1:
#include<stdio.h>
#include<stdbool.h>//including standard library
int main()
{
    bool c=false;//declaring boolean variable hence c gets 0
    if(c==true)//condition fails
    {
        printf("The value of c is %d",c);
    }
    else
    {
        printf("The value of c is %d",c);//prints value 0
    }
    return 0;
}

The value of c is 0

This bool variable when declared with True it takes value as “1” and when declared with False it takes values as “0”.Other than including stdbool library .These are boolean values are declared in other different methods.

Method 2:

These boolean variables are declared using typedef enum function ,where in enum function first value declared will be 0 and increments its values to next values .Here is the example of using enum function .

#include<stdio.h>
typedef enum{true,false} b;//after declaring values of true=0 and false=1
int main()
{
    b c=true;//c will get value 0
    if(c==true)
       printf("The value of c is %d ",c);
    else
       printf("The value of c is %d ",c);
    return 0; 
}

The Value of c is 0

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Also Read:

Initializing all members of array with same value

Tags: