C Programs Tutorials | IT Developer
IT Developer

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.

  1. 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;

}

  1. _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;

}

  1. 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

  1. Logical Operators:
    • && (AND), || (OR), ! (NOT).
    • Works with all types used to represent Booleans.
  2. 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>

  1. Improved Readability:
    • Code becomes more intuitive using bool, true, and false.
  2. Standardized Representation:
    • Consistent Boolean behavior across different compilers and environments.
  3. 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.