- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Booleans
![]() Share with a Friend |
C Programming - C Booleans
In C, there is no direct Boolean data type in the original C language (C89/C90 standards). However, with the introduction of the C99 standard, the _Bool type and <stdbool.h> library were added to represent Boolean values more explicitly.
- Representation of Booleans in C
Before C99, integers (int) were commonly used to represent Boolean values:
- 0 represents false.
- Any non-zero value represents true.
Example:
C
#include <stdio.h>
int main() {
int flag = 1; // True
if (flag) {
printf("True\n");
} else {
printf("False\n");
}
return 0;
}
- _Bool Type (C99)
The _Bool type was introduced in the C99 standard as a keyword to represent Boolean values. It can hold only two values:
- 0 for false.
- 1 for true.
Example:
C
#include <stdio.h>
int main() {
_Bool flag = 1; // True
if (flag) {
printf("True\n");
} else {
printf("False\n");
}
return 0;
}
- Using <stdbool.h> Library
The <stdbool.h> header file provides a more readable and programmer-friendly way to use Booleans. It defines:
- bool as an alias for _Bool.
- true as 1.
- false as 0.
Example:
C
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isAvailable = true;
if (isAvailable) {
printf("The item is available.\n");
} else {
printf("The item is not available.\n");
}
return 0;
}
Comparison of Boolean Implementations
| Feature | Using _Bool | Using <stdbool.h> | Using int |
|---|---|---|---|
|
Readability |
Less readable |
More readable (uses bool) |
Less readable |
|
Standard |
C99 and later |
C99 and later |
Works in all C versions |
|
Additional Features |
None |
Defines true and false |
None |
Operations with Booleans
- Logical Operators:
- && (AND), || (OR), ! (NOT).
- Works with all types used to represent Booleans.
- Comparison Operators:
- ==, !=, <, >, <=, >=.
Example:
C
#include <stdio.h>
#include <stdbool.h>
int main() {
bool x = true, y = false;
printf("x && y: %d\n", x && y); // Output: 0
printf("x || y: %d\n", x || y); // Output: 1
printf("!x: %d\n", !x); // Output: 0
return 0;
}
Example Program with Booleans
C
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isEven;
int number;
printf("Enter a number: ");
scanf("%d", &number);
isEven = (number % 2 == 0); // Evaluate if the number is even
if (isEven) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}
return 0;
}
Advantages of Using <stdbool.h>
- Improved Readability:
- Code becomes more intuitive using bool, true, and false.
- Standardized Representation:
- Consistent Boolean behavior across different compilers and environments.
- Prevention of Errors:
- Helps avoid errors caused by treating non-zero values as true.
Key Points
- Use <stdbool.h> for modern and readable C code.
- In legacy code, int can be used to represent Boolean values.
- Logical and comparison operators work seamlessly with all Boolean representations.
Booleans are simple yet crucial for decision-making and logical operations in C programming.
