The C language standard (C89) does not define a Boolean type. If you use true and false, the following error will occur:

infinite.c:5:12: error: use of undeclared identifier 'true'
    while (true) {

1 error generated.
make: *** [infinite] Error 1

We can use C language macro definitions to set it up:

// Macro definition for Boolean type #define BOOL int #define TRUE 1 #define FALSE 0 // Define a Boolean variable BOOL flag = FALSE;

It can also be defined using an enum:

typedef enum { true=1, false=0 }bool;

Example

#include <stdio.h> // Calculate n!, the value of n is defined in main int main(void) { int n = 10; // Calculate the factorial number int sum = 1; // Used to store the factorial result typedef enum { true=1, false=0 }bool; bool flag = false; // Factorial marker int num = n; // Number of loop iterations while( !flag ) { sum = sum * (num--); // End the loop when num = 1 if( num == 1) { flag = true; } } printf ("The factorial value of %d is %d\n", n, sum); return 0; }

The output result is:

10的叠乘值为 3628800 

C99 also provides a header file <stdbool.h> that defines bool to represent _Bool, true to represent 1, and false to represent 0. As long as you include stdbool.h, you can easily work with the Boolean type.

Example

// Include stdbool.h to use the Boolean type #include <stdbool.h> #include <stdio.h> // Calculate n!, the value of n is defined in main int main(void) { int n = 10; // Calculate the factorial number int sum = 1; // Used to store the factorial result bool flag = false; // Factorial marker int num = n; // Number of loop iterations while( !flag ) { sum = sum * (num--); // End the loop when num = 1 if( num == 1) { flag = true; } } printf ("The factorial value of %d is %d\n", n, sum); return 0; }

The output result is:

10的叠乘值为 3628800