C Programming

How to Use Boolean Value in C

Boolean values in C are quite common, and understanding their use can enable you to make your code more efficient and easier to read. A Boolean value may be utilized in a software to make judgments since it can be either true or false. It is one of the fundamental Data Types in C.

Boolean values are useful in many different contexts ranging from loop control and conditionals to memory allocation and data structure implementation. When used with logic operators, such as “AND”, “OR” and “NOT,” Boolean values can create complex expressions that can be used to control the behavior of an application or to test conditions or even make decisions.

This article is a detailed guide to use Boolean value in C programming.

Use Boolean Value in C

You can use Boolean values in C programming language either with header and data type or without them. Let’s get into details of both these methods.

Method 1: Use Boolean Value with Header and Data Type

To use Boolean value through this method, the first step is to include the header file with the name “stdbool.h”. After the main body, the users have to define the variable “bool” that defines a variable of type Boolean. This variable can store either 0 or 1, representing true and false statements respectively.

Look at a simple example now to better understand how the Boolean data type in C is used.

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

int main() {
  bool a = true;
  if (a == true) {
    printf("The value of a is true");
  } else {
    printf("The value of a is false");
  }

  return 0;
}

 

In the above code, we defined a variable of type Boolean using the bool keyword and initialized it with the value true. After that, we used the if-else block to apply the condition tests to see if the value of variable “a” is true or false.

Output

Method 2: Use Boolean Value Without Using Boolean Header File and Data Type

Boolean values can also be defined without using Boolean header file and data type. In this case, we will need to develop a new data type that behaves just as in the preceding example.

Logical operators are connected to the Boolean type of value. The C language has three different categories of logical operators:

    • Two operands are accepted by the logical operator && (AND Operator). If both operand values are true, this operator returns true; otherwise, it returns false.
    • The || (OR Operator) logical operator takes two operands. If the values of both operands are false, it returns false; otherwise, it returns true.
    • Only one operand is accepted by the NOT operator with the operand “!” If the operand’s value is true, it returns false and vice versa.

We don’t need to use pre-defined functions to implement Bool. Let’s look at an example.

#include <stdio.h>

int main() {
    int x, y;
    printf("Type two integers: \n");
    scanf("%d%d", &x, &y);
    int x_positive = (x > 0);
    int y_positive = (y > 0);
    if (x_positive && y_positive) {
        printf("Both values are positive.\n");
    } else if (x_positive || y_positive) {
        printf("One of the values is positive.\n");
    } else {
        printf("Both values are negative.\n");
    }
    return 0;
}

 

In the above code, we are using two variables x and y, and checking if they are positive or negative. If both variables are positive (which can be checked by AND operator), the code prints “Both values are positive”. If one of them is negative, the code outputs (which can be checked by OR operator) “One of the values is positive”. If both are negative, the code prints the output, “Both values are negative”.

Output

Conclusion

Boolean variables provide a powerful, efficient way of controlling the flow of code, and can be used in conjunction with other data types for more complex tasks such as memory allocation and data structure manipulation. The users can use Boolean value with header file and data type or without them. Both methods are already discussed in the above-mentioned guidelines.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.