How to Use Boolean Values in C

Mehvish Ashiq Feb 02, 2024
  1. Boolean Values in C
  2. Use the bool Type for Boolean Values in C
  3. Use enum for Boolean Values in C
  4. Use typedef and #define for Boolean Values in C
How to Use Boolean Values in C

Today’s tutorial demonstrates how to employ bool, enum, typedef, and #define to use boolean values in the C programming language.

Boolean Values in C

The bool (a boolean data type) is natively available in C99 and newer; we must include the stdbool.h library to use this data type. In C99, the native data type is known as _Bool.

On the other hand, the bool is a standard library macro defined in the stdbool.h. The objects of the _Bool data type contain either 1 or 0, while the true and false are the macros from the stdbool.h library.

This implies that a C preprocessor will interpret #if true as #if 1 unless the stdbool.h is included in the C program. Meanwhile, the C++ preprocessor must recognize true natively as a language literal.

Use the bool Type for Boolean Values in C

Example code:

// library to use `bool`
#include <stdbool.h>
// library to use `printf()`
#include <stdio.h>

// main function
int main() {
  bool variableX = true;

  if (variableX) {
    printf("The value of 'variableX' is True");
  } else {
    printf("The value of 'variableX' is False");
  }
  return 0;
}

Output:

The value of 'variableX' is True

If we have C99 or newer, we can use the bool, an alias to the _Bool. We must include a library (also addressed as a header file) named stdbool.h to use bool; otherwise, we will get an error.

See the program given above as a practical example of using the bool type.

Use enum for Boolean Values in C

Example code:

// library to use `printf()`
#include <stdio.h>

// enum's declaration
typedef enum { false, true } boolean;

// main function
int main() {
  boolean variableX = false;

  if (variableX) {
    printf("The value of 'variableX' is True");
  } else {
    printf("The value of 'variableX' is False");
  }
  return 0;
}

Output:

The value of 'variableX' is False

The code snippet given above is a solution for those using C89/90 or who do not want to use the native bool type. We can use the enum function to create the bool type.

The enum (also called Enumeration) is a user-defined data type in C programming utilized to assign names to integral constants; why? The names make the program easy to read, understand, and maintain.

We use the boolean values by creating a new name for the bool type. For that, we are using the typedef keyword, which is used to give a new name to a type (see the example code for this section where we are providing a new name to the bool type that is boolean).

Once we execute the code given above, one enum will be created as the bool type. Further, put the elements of enum as false and true.

The first and second positions will be occupied by false to hold 0 and true to hold 1.

Use typedef and #define for Boolean Values in C

We can use the boolean values in the following two ways as an alternative to the second approach where we are using enum for boolean values.

Example code 1:

// library to use `printf()`
#include <stdio.h>
// declare a variable of `int` type
typedef int boolean;
// declare enum
enum { false, true };

// main function
int main() {
  boolean variableX = true;

  if (variableX) {
    printf("The value of 'variableX' is True");
  } else {
    printf("The value of 'variableX' is False");
  }
  return 0;
}

Example code 2:

// library to use `printf()`
#include <stdio.h>
// declare a variable of `int` type
typedef int boolean;
// define macros
#define true 1;
#define false 0;

// main function
int main() {
  boolean variableX = true;

  if (variableX) {
    printf("The value of 'variableX' is True");
  } else {
    printf("The value of 'variableX' is False");
  }
  return 0;
}

Both code examples produce the same result given below.

Output:

The value of 'variableX' is True

In example code 2, we are using #define (also called macro directive), which is a preprocessor directive used to define macros in a C program.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook