×

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

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

While-Loop in C

Syntax of While Loop The syntax that has been used for the while loop in C programming language is: while (termination condition) {   // the body of the loop  } Working of While-Loop Initially, we...

3 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Merge and Merge sort with example in C

Assume we have two ordered Integer arrays, a[] and b[]. If we wish to combine them into another ordered array, such as c[], we can use the following straightforward technique. Compare...

3 minutes read.

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.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.