- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- 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
- 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
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- 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
Booleans in C
Unlike the int, char or float types, the ANSI C standard doesn’t 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!