How to understand the Boolean type of C language
This article mainly explains "how to understand the Boolean type of C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the Boolean type of C language.
The C language standard (C89) does not define Boolean types. If you use true and false, the following errors will occur:
Infinite.c:5:12: error: use of undeclared identifier 'true' while (true) {1 error generated.make: * [infinite] Error 1
We can use the macro definition of C language to set up:
/ / Macro defines the Boolean type # define BOOL int#define TRUE 1#define FALSE 0 / / defines a Boolean variable BOOL flag = FALSE
It can also be defined by enumerating classes:
Typedef enum {true=1, false=0} bool
Example
# the value of include// computing n defines int main (void) {int n = 10 in main; / / calculates the superposition multiplier int sum = 1; / / used to store the result of superposition typedef enum {true=1, false=0} bool; bool flag = false; / / superposition tag int num = n / / number of cycles while (! flag) {sum = sum * (num--); / / end loop if when num=1 (num= = 1) {flag = true;}} printf ("% d's superposition value is% d\ n", n, sum); return 0;}
The output is as follows:
The multiplication of 10 is 3628800.
C99 also provides a header file that defines bool for _ Bool,true for 1pm false for 0. As long as you import stdbool.h, you can easily manipulate Boolean types.
Example
/ / Import stdbool.h to calculate the value of n using the Boolean type # include#include// define int main (void) {int n = 10; / / calculate the stack multiplier int sum = 1 in main; / / store the result of superposition bool flag = false; / / superposition tag int num = n; / / number of cycles while (! flag) {sum = sum * (num--) / / end loop if (num= = 1) {flag = true;}} printf when num=1 (superposition value of "% d is% d\ n", n, sum); return 0;}
The output is as follows:
The multiplication value of 10 is 3628800. I believe you have a deeper understanding of "how to understand the Boolean type of C language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!