Table of contents
1.
Introduction
2.
What is boolean in C?
3.
Why Do We Need Boolean Values?
4.
Boolean Data Type in C
4.1.
Syntax
5.
1. Using Header File stdbool.h
5.1.
C
6.
2. Using the Enumeration Type
6.1.
C
7.
Boolean Arrays in C
7.1.
C
8.
Boolean with Logical Operators
8.1.
C
9.
Short-Circuiting
10.
Using Boolean in Conditional Statements
10.1.
C
11.
Using bool in Loops
11.1.
C
12.
Using bool as a Function Return Type
12.1.
C
13.
Frequently Asked Questions
13.1.
Is Boolean 1 or 0 in C?
13.2.
How to display a Boolean in C?
13.3.
What is a boolean in programming?
14.
Conclusion
Last Updated: Sep 7, 2024
Easy

boolean in c

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In C, the boolean data type is used to represent logical values of either true or false. It is a fundamental type that allows for conditional statements and logical operations, enabling programmers to make decisions and control the flow of their programs based on specific conditions. In this article, we will discuss every type in detail with proper syntax and code.

Boolean in C

What is boolean in C?

Boolean in C is a fundamental data type that can hold one of two values: true or false. Bool in c used to represent logical values and is commonly used in programming to control the flow of execution in decision-making statements such as while loops, and for loops, and, if-else statements.

Why Do We Need Boolean Values?

Boolean values are essential in programming as they represent the binary states of truth: true and false. They are the foundation upon which conditional statements and loops are built, allowing for the execution of code based on logical conditions. Without Boolean values, programmers would struggle to implement simple checks and controls, leading to complex and less readable code.

Also read, Bit stuffing program in c

Boolean Data Type in C

With the C99 standard, the Boolean data type was officially introduced in C through the stdbool.h header. This inclusion brought about a more expressive way to handle true and false conditions in the language.

#include <stdbool.h>

int main() {
    bool isSuccessful = true;
    if (isSuccessful) {
        // Actions to perform if the condition is true
    }
}

Syntax

To declare a Boolean variable in C, you first include the stdbool.h header and then use the bool keyword as shown below:

#include <stdbool.h>
bool isComplete;

1. Using Header File stdbool.h

Utilizing the stdbool.h header, a Boolean in C can be demonstrated as follows:

  • C

C

#include <stdio.h>

#include <stdbool.h>




int main() {

   bool isEven = false;

   int number = 10;




   if (number % 2 == 0) {

       isEven = true;

   }




   printf("Is the number even? %s\n", isEven ? "Yes" : "No");

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Output 

Output

2. Using the Enumeration Type

In scenarios where stdbool.h is not preferred or available, you can define a Boolean type using typedef:

  • C

C

#include <stdio.h>




typedef enum { FALSE, TRUE } Bool;




int main() {

   Bool hasPassed = TRUE;

   printf("Has the student passed? %s\n", hasPassed ? "Yes" : "No");

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Output

Output

Boolean Arrays in C

Boolean arrays are useful for tracking multiple states or conditions. Here's how to declare and utilize a Boolean array in C:

  • C

C

#include <stdio.h>

#include <stdbool.h>




int main() {

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

   // Iterate and print the state of each flag

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

       printf("Flag %d is %s\n", i, flags[i] ? "set" : "not set");

   }

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Output

Output

Boolean with Logical Operators

In C, logical operators are used to form compound Boolean expressions. These operators are the building blocks of complex decision-making in code:

  • AND (&&): This operator returns true if both operands are true. It's a way to compound conditions that must all be satisfied for an action to take place.
     
  • OR (||): This operator returns true if at least one of the operands is true. It allows for flexibility in conditions, where multiple paths may lead to the same outcome.
     
  • NOT (!): This unary operator inverts the truth value of its operand. It's often used to check if a condition is not met.

These operators are used in if statements, loops, and anywhere else where you need to evaluate multiple conditions at once. Here's an example that uses all three:

  • C

C

#include <stdio.h>

#include <stdbool.h>




int main() {

   bool hasHighTemperature = false;

   bool hasSymptoms = true;




   // Check if medical attention is needed

   if (hasHighTemperature && hasSymptoms) {

       printf("Seek medical attention.\n");

   } else if (hasHighTemperature || hasSymptoms) {

       printf("Monitor and rest.\n");

   } else if (!hasSymptoms) {

       printf("No symptoms, continue to observe.\n");

   }

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Output

Output

In this example, the logical AND (&&) operator is used to determine if both conditions are true, which would necessitate immediate medical attention. The logical OR (||) operator allows for a more lenient condition, suggesting monitoring if either condition is true. Lastly, the NOT (!) operator is used to check the absence of symptoms.

Short-Circuiting

Short-circuiting is a logical operation optimization where evaluation stops as soon as the outcome is determined. For the AND operator (&&), if the first operand is false, the overall expression cannot be true, so the second operand is not evaluated. Similarly, for the OR operator (||), if the first operand is true, the overall expression must be true, and again, the second operand is not evaluated.

This behavior not only optimizes performance but also prevents the execution of potentially unsafe operations that could occur in the second operand.

Using Boolean in Conditional Statements

Let us look at an example to understand Boolean in Conditional Statements:

  • C

C

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

int main() {
bool is_sunny = true;

if (is_sunny) {
printf("It's a sunny day!\n");
} else {
printf("The weather is not sunny.\n");
}

int temperature = 25;
const char* weather_condition = (temperature > 20) ? "Warm" : "Cool";
printf("The weather is %s.\n", weather_condition);

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

output

Using bool in Loops

Let us look at an example to understand Boolean in Loops:

  • C

C

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

int main() {
bool is_playing = true;
while (is_playing) {
printf("Keep playing...\n");
char user_input[3];
printf("Do you want to continue playing? (yes/no): ");
scanf("%s", user_input);
is_playing = strcmp(user_input, "yes") == 0;
}

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

output

Using bool as a Function Return Type

Let us look at an example to understand bool as a Function Return Type:

  • C

C

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

bool is_prime(int number) {
if (number < 2) {
return false;
}
for (int i = 2; i * i <= number; ++i) {
if (number % i == 0) {
return false;
}
}
return true;
}

int main() {
// Using the function
int num = 11;
bool result = is_prime(num);
printf("Is %d a prime number? %s\n", num, result ? "Yes" : "No");

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

output

Also read, odd or even program in c

Frequently Asked Questions

Is Boolean 1 or 0 in C?

In C, Boolean values are represented as 1 for true and 0 for false, using the stdbool.h library.

How to display a Boolean in C?

To display a Boolean in C, use the printf function with %d to print true as 1 and false as 0.

What is a boolean in programming?

A boolean in programming is a data type that can hold one of two values: true or false, used for logical operations.

Conclusion

Booleans and logical operators in C are pivotal for creating clear and efficient conditional logic. The introduction of stdbool.h in C99 has made it easier for programmers to express Boolean logic, but the language's flexibility also allows for custom implementations. Understanding these concepts is crucial for any C programmer aiming to write robust and maintainable code.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass