C Standard Library<stdbool.h>
Before the C99 standard, integer types (such asint)to represent boolean values. For example,0represents false, non-zero values (usually1) were commonly used in C to represent true. This approach, while feasible, lacked intuitiveness and type safety. To address this, the C99 standard introducedstdbool.hheader file, which defines the boolean type and related macros.
<stdbool.h>is a standard header file in C, which defines the boolean type and its related constants. It makes the C boolean type (bool) became more explicit and usable, avoiding the traditional practice of using integers (such as 0 or 1) to represent boolean values.
stdbool.hThe header file defines the following:
bool: Boolean type, used to declare boolean variables.true:a macro representing a true value, usually defined as1。false:a macro representing a false value, usually defined as0。__bool_true_false_are_defined:a macro used to indicatetrueandfalseWhether it is defined.
These macros are defined as follows:
#define bool _Bool #define true 1 #define false 0 #define __bool_true_false_are_defined 1
bool is an alias for the _Bool type.
_Bool is a native type introduced in the C99 standard, representing a boolean value. The _Bool type can only store 0 or 1, making it suitable for storing logical values.
#include <stdbool.h> _Bool is_valid = 1; // 也可以直接使用 _Bool 类型
To use the boolean type and related macros in a C program, first you need to include<stdbool.h>Header file:
#include <stdbool.h> bool is_raining = true; bool is_sunny = false;
Example
The following is an example using<stdbool.h>a simple example:
Example
#include <stdbool.h>
int main() {
// Declare boolean variable
bool isReady = true;
bool isFinished = false;
// Use boolean variable
if (isReady) {
printf("The system is ready.\n");
} else {
printf("The system is not ready.\n");
}
if (!isFinished) {
printf("The task is not finished.\n");
}
// Boolean variable value
printf("isReady: %d\n", isReady); // Output 1 (true)
printf("isFinished: %d\n", isFinished); // Output 0 (false)
return 0;
}
Output result:
The system is ready. The task is not finished. isReady: 1 isFinished: 0
bool type
Define boolean variables using the bool type:
Example
#include <stdbool.h>
int main() {
bool flag = true; // Use bool type
if (flag) {
printf("Flag is true.\n");
} else {
printf("Flag is false.\n");
}
return 0;
}
In the above example, flag is a boolean variable with an initial value of true. Check whether flag is true, then print the corresponding message.
true and false macros
true and false are predefined macros, corresponding to boolean values 1 and 0 respectively, and can be used directly in conditional judgments:
Example
#include <stdbool.h>
int main() {
bool is_logged_in = false;
// Change boolean value
if (!is_logged_in) {
is_logged_in = true;
printf("User logged in: %s\n", is_logged_in ? "true" : "false");
}
return 0;
}
Used in conjunction with statements such as if and while
Boolean types are useful in control structures such as if, while, and for:
Example
#include <stdbool.h>
int main() {
bool condition = false;
while (!condition) {
printf("The condition is false.\n");
condition = true; // Change condition to true
}
return 0;
}
Here, the while loop will continue executing until condition becomes true.
Compatibility between the boolean type and integers
Although the bool type itself is a dedicated type, at the underlying implementation level it typically relies on an integer type (usually int). Therefore, true can be considered 1, and false is 0.
However, when programming, you should avoid mixing boolean values with ordinary integers, and try to maintain type clarity and readability.
Example
#include <stdbool.h>
int main() {
bool is_active = true;
// Wrong approach: mixing boolean values with integers
int status = is_active; // This line compiles fine, but it is best to avoid it
if (status) {
printf("Status is true.\n");
} else {
printf("Status is false.\n");
}
return 0;
}
Although the above code executes correctly, it is not recommended, because it mixes boolean values and integer types, making the code difficult to understand.
Advantages of the boolean type
- Readability: Use
boolThe type makes the program's intent clearer, compared to usingintThe type represents true or false,boolThe type can clearly indicate that the variable is only used to represent a boolean value. - Standardization:
<stdbool.h>It provides a standard way to handle boolean values, avoiding the complexity of using macros or integer types to represent boolean values. - Type Safety: with
intThe type is different,boolThe type is specifically used for logical judgment, reducing problems of type mismatch.