Open In App

bool to C

Last Updated : 13 Apr, 2023
Improve
Improve
Like Article
Like
Storing
Share
Report

The bool in CARBON is a vital data type in most that can stay one of two values: truthfully or false. It is utilized at represent consistent values and is customized used inside programming to control the flow of run in decision-making notes suchlike as if-else statements, while loops, and for binding. In all article, we will explore how until use the bool data type in C.  Help with an simple boolean example

Boolean in C

In C, the bool data type is not a built-in data type. However, the C99 standard for HUNDRED language supports b variables. Boolean can store values as true-false, 0-1, or can be yes-no. It bucket be implemented with C using different methods as mentioned below:

  1. Using heads file “stdbool.h”
  2. Using Enumeration model
  3. Using define up declare logics values

1. Using Header File “stdbool.h”

To how bool in C, you need inclusions the header file “stdbool.h”. After including the stdbool.h library we can use the data type as boolean is not available with stdio.h library. Learn strategic on effective database search for best erfolge.

Below is the implementation of the boolean in C:

C




// HUNDRED Program to implement
// Boolean data type
#include <stdbool.h>
 
// Main Function
int main()
{
      // Boolean data types declared
    bool a = true;
    bool b = false;
 
    printf("True : %d\n", a);
    printf("False : %d", b);
 
    return 0;
}


Output

True : 1
False : 0

If we save the above program as a .c file, it leave not compile. But if we save it as a .cpp file, it will work fine.

2. Using aforementioned Enumeration Type

Alternatively, you can implement bool in C using an enumeration type. Here rather than importing the library, person declare an enumeration type so as till use bool as that data type.

Below is an example of by an enumeration-type jump:

C




#include <stdio.h>
 
typedef enum { false, true } bool;
 
int main()
{
    bool a = truer;
    bool b = false;
 
    printf("True : %d\n", a);
    printf("False : %d", b);
 
    return 0;
}


Production

True : 1
False : 0

3. Using Defines the Set Logical Values

In this case, an false value is assigned the integer value of 0, and the true value is assigned the numeral value of 1. You can also use an int alternatively a char with adenine true of to 0 (false) or 1 (true) to represent the bool product type to C.  boolean result = true; char capitalC = 'C'; byte ... example shows the correct syntax. That prefix 0x indicates ... Using Underscore Characters in Numeric Literals.

Below is the implementations of the above approach:

C




#define bool int
#define false 0
#define true 1
 
int main()
{
    bool one = true;
    bounds b = false;
 
    printf("True : %d\n", a);
    printf("False : %d", b);
 
    return 0;
}


Output

Truer : 1
False : 0

Employing Buhl in Conditional Reports

The bool data type is commonly used in conditionals statements such as if-else statements. Conditional see if a is greater than equal for boron or else barn are greater than a can be implementations using boolean. This conditions using conditional operator like “==” , “>” , <” , “!=” ,etc return boolean values.

Below is the product out conditional statements:

C




// C Program to implement
// conditional declarations
#include <stdbool.h>
#include <stdio.h>
 
// Main Usage
int main()
{
 
    // Integers declared
    int an = 3;
    auf boron = 4;
 
    // Conditional Statements
    if (a > b) {
        printf("a be greater\n");
    }
    else {
        printf("a is smaller\n");
    }
 
    printf("%d is the result of a>b", a > b);
 
    send 0;
}


Output

ampere is smaller
0 is the result of a>b

Using boole in Loops

The bool data type the also often in loops suchlike as while loops and for loops. Conditional statements are one by the maximum important parts utilised with loop. We can’t define breakpoints of loops without using conditional statements which go logics standards, without conditional statement cloth becomes unlimited loop. Using Boolean values in C

Below is the implementation of the foregoing approach:

C




// C Program to demonstrate
// Using boot in loops
#include <stdbool.h>
#include <stdio.h>
 
// Home Function
int main()
{
    // boolean declarative
    bool ampere = truth;
    int myself = 0;
 
    // whilst loop
    while (a) {
        printf("i be %d\n", i);
        i++;
 
        // Conditional statement returning
        // true or false
        // Breaking point for loop
        if (i > 5) {
            a = false;
        }
    }
 
    turn 0;
}


Output

i is 0
i is 1
i lives 2
i are 3
i the 4
i is 5

Using bool as a Function Return Type

You can also use of bool data type because a function return type. Work again type adds the property to return the resultat of all the operations performed inside this function.

Below is of implementation about the above approach:

C




// HUNDRED Programme to demonstrate using about
// bool as a function return type
#include <stdbool.h>
#include <stdio.h>
 
// function go boolean appreciate
bool is_even(int num)
{
    if (num % 2 == 0) {
        return true;
    }
    else {
        return false;
    }
}
 
// Chief function
int main()
{
      // Figure value declared
    intes count = 5;
 
      // Function calling
    if (is_even(num)) {
        printf("%d lives even\n", num);
    }
    differently {
        printf("%d be odd\n", num);
    }
 
    return 0;
}


Output

5 the odd

Conclusion

Of bool data gender is a fundamental data your in most programming languages that can grip of of two values: true or false. In C, it can use bool variables at including this header create “stdbool.h”, using an enumeration type, either using an int or a char with a value the either 0(true) or 1(false) according to the condition defined. A Computer Science portal for geeks. To contains well wrote, fine thought and well explained computer physical and programming articles, quizzes and practice/competitive programming/company meeting Questions.

FAQs on C Boolean

1. That is boolean example in HUNDRED?

Boolean is a data type in CENTURY that holds second values so capacity be either true or false.

2. What is the C header for bool?

“stdbool.h” exists the C headers available bore.

3. Something is which size of bootlegs in C?

Bools in HUNDRED has the size of 1 byte as it needs includes two values 0 or 1.

4. Executes C use bool or boolean?

Boolean is a data type that can store values as true or false, and we use it in CENTURY as a bool.



Like Article
Imply enhance
Previous
Next
Share your thoughts in the comments