×

Booleans in C

Introduction to Boolean

With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in C programming. Boolean values are true or false but often get misunderstood in C programming. In this article, we'll explain Booleans and how we can use them in C programming. We'll also discuss how we can use them to create logical conditions and how they fit into the bigger programming picture. At the end of this article, you will have an understanding about the way to use Booleans in C programming and how to apply them to resolve more complex programming issues.

History of Boolean

Boolean, also known as the logical, is a primitive data type in C programming language. It is used to present two possible outcomes: true or false. Before C99, there was no native Boolean data type in C. Instead, programmers had to use '#define' or 'typedef' to define their Boolean types.

For example, you could define a Boolean type using #define as follows:

#define bool int
#define true 1
#define false 0

Or, you could use 'typedef' to define a Boolean type as follows:

typedef int bool;
#define true 1
#define false 0

However, these methods had some limitations, such as not being able to use 'sizeof' to determine the size of a Boolean type or not using Boolean values in specific contexts, such as return types or in the parameter of the function.

To address these issues, C99 introduced the '_Bool' type, a native Boolean data type in C. It is defined in the '<stdbool.h>' header file, and it can hold the values 'true' or 'false', which are represented as '1' and '0' respectively.

In addition to '_Bool', C99 also introduced the bool type, a synonym for '_Bool'. It allows you to use the 'bool' type in your code, which can be easier to read and understand, especially if you are coming from a language that has a native Boolean type.

Example:

#include <stdbool.h>
#include <stdio.h>
int main() 
{
  bool flag = true;
  printf("Value of flag: %d\n", flag);


  flag = false;
  printf("Value of flag: %d\n", flag);


  return 0;
}

Output:

Value of flag: 1
Value of flag: 0

What is Boolean in C?

Boolean in C is a data type used to store data values with only two possible states, either true or false. It is also a logical data type representing various logical conditions, such as true or false questions or on off switches. They are used to test for certain conditions, whether an input is valid or meets the situation. Boolean values are a fundamental building block of code used in programming languages to evaluate expressions and build logic into programs.

In C language, there is a Boolean data type in C. In C++, you can directly use Boolean as it is but in C language, you have to use the header <stdbool.h>. If you do not use the <stdbool.h> header file, the program will be interpreted or not compiled.

How to use the Boolean in C?  

Booleans are a fundamental data type in the C programming language. They are used to show the actual value of a statement, i.e. whether something is true or false. Boolean values can be assigned to variables and used in comparison and logic operations. In C, the Boolean values are represented by the integers 0 and 1. To use a Boolean in C, you must declare a variable of type Boolean and assign it a value of either 0 or 1. Boolean values can also be used in for conditionals statements, like if-else statements, to regulate the flow of a program. With their ability to represent true and false values, Booleans are an essential part of any C program.

The following program will help you in the creation of bool variables to use them in various Boolean operations.

Example:

#include <stdio.h>
#include <stdbool.h>
int main()
{
    //bool type variables
    bool a=true,b=true;
    //doing Boolean operations
    printf("Variable1&&Variable2 = %d\n\n", a&&b);
    printf("Variable1||Variable2 = %d\n\n", a||b);
    printf("!Variable1 = %d\n", !a);
    printf("!Variable2 = %d\n", !b);
    return 0;
}

Output:

Variable1&&Variable2 = 1


Variable1||Variable2 = 1


!Variable1 = 0
!Variable2 = 0

Boolean Arrays in C

A Boolean array is a data structure used in C programming that store Boolean values. Boolean values are the values that can either be true or false. Boolean arrays are often used in C programming to store multiple Boolean values in a single variable. Boolean arrays can have any number of elements, and each component can hold a true or false value. Boolean arrays can store data used to sets or make decisions in programming logic. Boolean arrays are a powerful C programming tool used to create efficient applications. Let us understand through an example.

The program given below is for finding the odd/even numbers from the given array, for which I statically assigned the values to the array with the help of the Boolean array. In the following program, true is an even number, and false is an odd number.

Example:

#include <stdio.h>
#include <stdbool.h>  // For using the Boolean data type.
int main( )
{
    int a[] = {5,3,9,8,7};   //integer array
    const int aSize = sizeof(a)/sizeof(a[0]);    //calculate array size
    // Declaration of Boolean array.
    //Denotes even and odd number of array //
    bool bool_a[aSize]


  // Boolean array initialization.
    int j;
    for (j= 0; j < aSize; ++j)
    {
        if ((a[j]%2) == 0)
        {
            //even number
            bool_a[j] = true;
        }
        else
        {
            //odd number
            bool_a[j] = false;
        }
    }
 // Printing Values of Boolean array.
    for (j= 0; j < aSize; ++j)
    {
        printf("a[%d]=%d\n",j,bool_a[j]);
    }
    return 0;
}

Output:

a[0]=0
a[1]=0
a[2]=0
a[3]=1
a[4]=0

Time complexity of Booleans in C

The time complexity of Boolean values in C depends on how they are used in a program. In general, the time complexity of a Boolean value is constant, meaning it does not depend on the size of the input. This is because a Boolean value is a single bit of information that can only be either true or false, and accessing or manipulating it takes a constant amount of time.

However, the time complexity of an operation that uses a Boolean value may not be constant.

In the above C program, the time complexity of the Boolean value is_even is constant, but the time complexity of the operation that uses it (the if-else statement) is not. The time complexity of the if-else statement depends on the time complexity of the Boolean expression (num % 2 == 0), which is used to determine the value of is_even.

In this case, the time complexity of the Boolean expression is constant because the modulo operator (%) has a constant time complexity. However, if the Boolean expression contained an operation with a higher time complexity, such as a loop or a recursive function, the time complexity of the if-else statement would also increase.

In conclusion, the time complexity of Boolean values in C is constant, but the time complexity of operations that use Boolean values may not be constant and may depend on the time complexity of the Boolean expressions they contain.

Space Complexity of Booleans in C

The space complexity of Boolean values in C is constant and does not depend on the size of the input. A Boolean value is a single bit of information that can only be either true or false, and it takes up a fixed amount of memory regardless of the size of the input. In C, Boolean values are represented by the _Bool type, which is a one-byte data type. This means that a Boolean value takes up 1 byte of memory, which is equivalent to 8 bit. The space complexity of Boolean values in C is constant and does not depend on the size of the input. However, the space complexity of operations that use Boolean values may not be constant and may depend on the space complexity of the code blocks they contain and any variables or data structures they use.


Related Topics

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Long int in C

We use data type to avoid the type of data to be stored for a variable at the time of declaration.So, we can say that the variable type is known...

3 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Calendar application in C

Introduction to the calendar application We all are familiar with the calendar. It plays a crucial role in our daily life. We run toward the calendar whenever we want to know...

6 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.