What is boolean in C?

Does C programming language support boolean type? If you are reading this article, I believe this question has also come to your mind what the boolean is, How to use bool in C, ..etc.

Don’t worry in this post I will explain the C boolean with the help of programming examples. But before going into detail I want to let you know that C supports boolean since C99.

 

What is boolean in C?

Before the C99 boolean type is not supported by the native C. The C99 Standard for the C language introduces the boolean data type in C. The boolean works as it does in C++ but in C you have to use the header <stdbool.h>. However, if you don’t include the header file​ <stdbool.h>, the program will not compile.

The header <stdbool.h> defines four macros. These are as followed,

  1. bool
  2. true
  3. false
  4. __bool_true_false_are_defined.

 

These C macros expand in the following format mentioned in below table:

Macro name Expands to
bool _Bool
true integer constant 1
false integer constant 0
__bool_true_false_are_defined integer constant 1

 

After watching the above table I believe you are able to understand all 4 macros. But still, some folks are thinking what is this _Bool?.

So for your information _Bool is a reserved keyword and an object declared as type _Bool is large enough to store the values 0 and 1.

 

How to use the boolean in C?

Now I believe you are familiar with boolean types in C. So it’s time to understand how to use it in programming. So let’s see an example code.

The below example code explains how to create bool variables and use them in boolean operation.

#include <stdio.h>
#include <stdbool.h>

int main()
{
    //bool type variables
    bool var1=true;
    bool var2=true;

    //doing boolean operations
    printf("var1&&var2 = %d\n\n", var1&&var2);

    printf("var1||var2 = %d\n\n", var1||var2);

    printf("!var2 = %d\n", !var2);

    return 0;
}

Output:

What is bool in C with example code_Aticeworld

 

Now I believe you know how to use the bool in C. But here is one problem, the problem is that all the above-mentioned macros will work only on C99 or newer compiler. They will not work on the compiler prior to C99.

Don’t worry if your compiler is an old one, you can implement your own boolean using enum in C. But better to use the standard one. See the below expression,

typedef enum
{
    myfalse = 0,
    mytrue = 1
} mybool;

 

To avoid name collision with a standard name, I have used different names in place of false, true, and bool.

 

Now you can use the mybool (enum) in your code. In mybool enum, myfalse hold 0, and mytrue holds 1. Below is an example code for better understanding.

#include <stdio.h>

//enum
typedef enum
{
    myfalse = 0,
    mytrue = 1
} mybool;


int main()
{
    mybool val = myfalse;
    if(val)
    {
        printf("val is true.");
    }
    else
    {
        printf("val is false.");
    }
    return 0;
}

Output:

val is false.

 

Boolean Arrays in C:

You can also create an array of bool types that can store multiple true and false values. You can access the true and false values of the boolean array using their indexes.

Let’s see an example where I am finding the even and odd numbers of the given integer array with the help of the boolean array. In the below code true means input array index has an even number and false means has an odd number.

#include <stdio.h>
#include <stdbool.h>  // As we are using boolean data type.
int main()
{
    //integer array
    int arr[] = {3,5,7,8,9};
    //calculate array size
    const int arraySize = sizeof(arr)/sizeof(arr[0]);

    // Declaration of boolean array.
    /* Denote even and odd number
       of given input array */
    bool bool_arr[arraySize];

    // Initialization of boolean array.
    for (int i = 0; i < arraySize; ++i)
    {
        if ((arr[i]%2) == 0)
        {
            //even number
            bool_arr[i] = true;
        }
        else
        {
            //odd number
            bool_arr[i] = false;
        }
    }

    // Printing elements of boolean array.
    for (int i = 0; i < arraySize; ++i)
    {
        printf("%d,",bool_arr[i]);
    }

    return 0;
}

Output: 0,0,0,1,0,

 

Recommended Articles for you: