- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape Sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if Statement
- C - if...else Statement
- C - if...else if Ladder
- C - Nested if Statements
- C - Switch Statement
- C - Nested Switch Statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Pointers in C
- C - Pointers
- C - Initialization of Pointer Arrays
- C - Applications of Pointers
- C - Dereference Pointer
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Pointer Arithmetics
- C - Pointers and Arrays
- C - Pointer vs Array
- C - Pointer to an Array
- C - Array of Pointers
- C - Pointers vs. Multi-dimensional Arrays
- C - Pointer to Pointer
- C - Chain of Pointers
- C - Character Pointers and Functions
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Array of Function Pointers
- C - Pointers to Structures
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- User-Defined Data Types
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Flexible Array Members in Structures
- C - Structures vs Unions
- Memory Management in C
- C - Memory Layout
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- C - Dynamic Array Resizing
- C - Memory Leaks
- File Handling in C
- C - File Handling
- C - Input & Output
- C - File Operations
- C - Formatted Output
- C - getc, getchar, getch, getche
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Macros
- C - Working of Preprocessor
- C - Preprocessor Operators
- C - Header Files
- C - Custom Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C - Online Compiler
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.
#include <stdio.h>
int main (){
enum bool {false, true};
enum bool x = true;
enum bool y = false;
printf ("%d\n", x);
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 −
#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
1 0
Example 2
We can even use the enumerated constants in the decision-making or loop statements −
#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
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 −
#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 −
#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 −
#include <stdio.h>
#include <stdbool.h>
int main(){
bool x;
x = 10 > 5;
if(x)
printf("x is True\n");
else
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 −
#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;
}
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!