Advertisement

Programmer Coding

Boolean in C Language

What is Boolean in C?

In C, Boolean is a data type that contains two types of values, i.e., 0 and 1. Basically, the bool type value represents two types of behavior, either true or false. Here, ‘0’ represents false value, while ‘1’ represents true value.

In C Boolean, ‘0’ is stored as 0, and another integer is stored as 1. We do not require to use any header file to use the Boolean data type in C++, but in C, we have to use the header file, i.e., stdbool.h. If we do not use the header file, then the program will not compile.

Advertisement
Syntax

bool variable_name;

In the above syntax, bool is the data type of the variable and variable_name is the name of the variable.

#include <stdio.h>

#include <stdbool.h>

int main() {

// Boolean variables using stdbool.h

bool bool_true = true;

bool bool_false = false;

// Conditional statements using boolean values

if (bool_true) {

printf(“The condition is true\n”);

} else {

printf(“The condition is false\n”);

}

if (!bool_false) {

printf(“The condition is true\n”);

} else {

printf(“The condition is false\n”);

}

// Using boolean variables in expressions

bool result = bool_true && bool_false;  // Logical AND operation

printf(“The result of logical AND operation: %d\n”, result);

return 0;

}

In the code above, we used the <stdbool.h> header file so that we could use bool type variables in the program. After declaring the header file, we create the bool variable “x” and assign it the value “false”. We then add the if..else statement to determine whether the value of “x” is true or not.

Output

The condition is true

The condition is true

The result of logical AND operation: 0

  • Boolean Array

Now we create an array of bool type. Boolean arrays can contain true or false values, and array values ​​can be accessed with the help of indexes.

Example of using a boolean array in C

#include <stdio.h>

#include <stdbool.h>

int main() {

// Declare a boolean array with 5 elements

bool bool_array[5] = {true, false, true, false, true};

// Print the elements of the boolean array

printf(“Boolean Array: “);

for (int i = 0; i < 5; i++) {

printf(“%d “, bool_array[i]);

Advertisement

}

printf(“\n”);

return 0;

}

In the above code, we have declared a Boolean type array containing two values, i.e., true and false.

Output

Boolean Array: 1 0 1 0 1

  • typedef

There is another way to use boolean values: typedef. Basically, typedef is a keyword used to name an existing data type in C language.

Syntax

typedef existing_data_type new_data_type_name;

Example

#include <stdio.h>

// Define a new name for the data type “int”

typedef int integer;

int main() {

// Using the new data type name

integer num = 10;

// Printing the value

printf(“The value of num is: %d\n”, num);

return 0;

}

In the code above, we used Boolean values ​​such as true and false, but we did not use the bool type. We use boolean values ​​by creating a new name of type “bool”. To achieve this, the typedef keyword is used in the program.

Output

The value of num is: 10

Boolean With Logical Operators

  • Boolean type values ​​are associated with logic concepts. Logical operators in the C language are divided into three groups:
  • && (AND operator): It is a two-operator dialog operator. This operator returns true if both functions are true, otherwise it returns false
  • || (or operator): An operator with two employees. If both operands evaluate to false, false is returned, otherwise true is returned.
  • ! (Not operator): It is not an operator who accepts only one employee. Returns true if the value of the operand is false; Returns false if the value of the operand is true.
Example

#include <stdio.h>

#include <stdbool.h>

int main() {

// Declare boolean variables

bool bool_var1 = true;

bool bool_var2 = false;

// Logical AND (&&) operator

bool result_and = bool_var1 && bool_var2;

printf(“Logical AND result: %d\n”, result_and);

// Logical OR (||) operator

bool result_or = bool_var1 || bool_var2;

printf(“Logical OR result: %d\n”, result_or);

// Logical NOT (!) operator

bool result_not = !bool_var2;

printf(“Logical NOT result: %d\n”, result_not);

return 0;

}

Output

Logical AND result: 0

Logical OR result: 1

Logical NOT result: 1

Leave a Comment

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

Scroll to Top