0% found this document useful (0 votes)
11 views7 pages

20 - C Programming Booleans

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

20 - C Programming Booleans

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Page 1 of 9

Home Whiteboard Online Compilers Practice Articles AI Assistant

SQL HTML CSS Javascript Python Java C C++ PHP Scala C#

Booleans in C

Unlike the int, char or float types, the ANSI C standard doesnt have a built-in or
primary Boolean type. A Boolean or bool data generally refers to the one that can hold
one of the two binary values: true or false (or yes/no, on/off, etc.). Even if the bool type
is not available in C, you can implement the behaviour of Booleans with the help of an
enum type.

The new versions of C compilers, complying with the C99 standard or later, support the
bool type, which has been defined in the header file stdbool.h.

Using enum to Implement Boolean Type in C


The enum type assigns user-defined identifiers to integral constants. We can define an
enumerated type with true and false as the identifiers with the values 1 and 0.

Example

1 or any other number that is not 0 represents true, whereas 0 represents false.

Open Compiler

#include <stdio.h>

int main (){

enum bool {false, true};


enum bool x = true;
enum bool y = false;

printf ("%d\n", x);

[Link] 1/9
Page 2 of 9

printf ("%d\n", y);


}

Output

Run the code and check its output −

1
0

typedef enum as BOOL


To make it more concise, we can use the typedef keyword to call enum bool by the
name BOOL.

Example 1

Take a look at the following example −

Open Compiler

#include <stdio.h>

int main(){

typedef enum {false, true} BOOL;

BOOL x = true;
BOOL y = false;

printf ("%d\n", x);


printf ("%d\n", y);
}

Here too, you will get the same output −

Output

[Link] 2/9
Page 3 of 9

1
0

Example 2

We can even use the enumerated constants in the decision-making or loop statements −

Open Compiler

#include <stdio.h>

int main(){

typedef enum {false, true} BOOL;

int i = 0;

while(true){
i++;
printf("%d\n", i);

if(i >= 5)
break;
}
return 0;
}

Output

When you run this code, it will produce the following output −

1
2
3
4
5

Boolean Values with #define

[Link] 3/9
Page 4 of 9

The #define preprocessor directive is used to define constants. We can use this to
define the Boolean constants, FALSE as 0 and TRUE as 1.

Example

Take a look at the following example −

Open Compiler

#include <stdio.h>

#define FALSE 0
#define TRUE 1

int main(){

printf("False: %d \n True: %d", FALSE, TRUE);

return 0;
}

Output

Run the code and check its output −

False: 0
True: 1

Boolean Type in stdbool.h


The C99 standard of C has introduced the stdbool.h header file. It contains the
definition of bool type, which actually is a typedef alias for _bool type. It also defines
the macros true which expands to 1, and false which expands to 0.

Example 1

We can use the bool type as follows −

Open Compiler

[Link] 4/9
Page 5 of 9

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

int main(){

bool a = true;
bool b = false;

printf("True: %d\n", a);


printf("False: %d", b);

return 0;
}

Output

On executing this code, you will get the following output −

True: 1
False: 0

Example 2

We can use bool type variables in logical expressions too, as shown in the following
example −

Open Compiler

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

int main(){

bool x;
x = 10 > 5;

if(x)
printf("x is True\n");
else

[Link] 5/9
Page 6 of 9

printf("x is False\n");

bool y;
int marks = 40;
y = marks > 50;

if(y)
printf("Result: Pass\n");
else
printf("Result: Fail\n");
}

Output
Run the code and check its output −

x is True
Result: Fail

Example 3
Let us implement a while loop with the help of a bool variable −

Open Compiler

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

int main(void){

bool loop = true;


int i = 0;

while(loop){
i++;
printf("i: %d \n", i);

if (i >= 5)
loop = false;

[Link] 6/9
Page 7 of 9

}
printf("Loop stopped!\n");
return EXIT_SUCCESS;
}

Output

When you run this code, it will produce the following output −

i: 1
i: 2
i: 3
i: 4
i: 5
Loop stopped!

TOP TUTORIALS

Python Tutorial

Java Tutorial
C++ Tutorial
C Programming Tutorial

C# Tutorial
PHP Tutorial
R Tutorial

HTML Tutorial
CSS Tutorial
JavaScript Tutorial

SQL Tutorial

TRENDING TECHNOLOGIES

Cloud Computing Tutorial


Amazon Web Services Tutorial

Microsoft Azure Tutorial


Git Tutorial
Ethical Hacking Tutorial

[Link] 7/9

You might also like