C Standard Library
Before the C99 standard, integer types (such as int) were commonly used in C to represent boolean values. For example,0 represents false, and a non-zero value (usually 1) represents true. Although this approach works, it lacks intuitiveness and type safety. To solve this problem, the C99 standard introduced the stdbool.h header file, which defines the boolean type and related macros.
is a standard header file in C that defines the boolean type and its related constants. It makes the C boolean type (bool) clearer and more usable, avoiding the traditional practice of using integers (such as 0 or 1) to represent boolean values.
stdbool.h The header file defines the following:
bool: The boolean type, used to declare boolean variables.true: A macro representing the true value, usually defined as1.false: A macro representing the false value, usually defined as0.__bool_true_false_are_defined: A macro used to indicate whethertrueandfalseis 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 hold 0 or 1, so it is suitable for storing logical values.
#include
_Bool is_valid = 1; // 也可以直接使用 _Bool 类型
To use the boolean type and related macros in a C program, you first need to include the header file:
#include
bool is_raining = true;
bool is_sunny = false;
Example
The following is an example of using A simple example of :
Example
#include
#include
int
main
(
)
{
// Declare boolean variables
bool isReady
=
true
;
bool isFinished
=
false
;
// Use boolean variables
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"
)
;
}
// Values of the boolean variables
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
The bool type
Define boolean variables using the bool type:
Example
#include
#include
int
main
(
)
{
bool flag
=
true
;
// Using the bool type
if
(
flag
)
{
printf
(
"Flag is true.\n"
)
;
}
else
{
printf
(
"Flag is false.\n"
)
;
}
return
0
;
}
In the above example, flag is a variable of boolean type with an initial value of true. It checks whether flag is true, then prints the corresponding message.
The true and false macros
true and false are predefined macros corresponding to the boolean values 1 and 0, respectively, and can be used directly in conditional checks:
Example
#include
#include
int
main
(
)
{
bool is_logged_in
=
false
;
// Change a boolean value
if
(
!
is_logged_in
)
{
is_logged_in
=
true
;
printf
(
"User logged in: %s\n"
,
is_logged_in
?
"true"
:
"false"
)
;
}
return
0
;
}
In this code, the initial value of is_logged_in is false, and it is then changed to true in the logic.
Used together with statements such as if and while
The boolean type is useful in control structures such as if, while, and for:
Example
#include
#include
int
main
(
)
{
bool condition
=
false
;
while
(
!
condition
)
{
printf
(
"The condition is false.\n"
)
;
condition
=
true
;
// Change the condition to true
}
return
0
;
}
Here, the while loop continues to execute until condition becomes true.
Compatibility of the boolean type with integers
Although the bool type is itself a dedicated type, at the implementation level it usually relies on an integer type (typically 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 keep types clear and readable.
Example
#include
#include
int
main
(
)
{
bool is_active
=
true
;
// Bad practice: mixing boolean values with integers
int
status
=
is_active
;
// This line compiles fine, but is best avoided
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 and integer types, making the code harder to understand.
Advantages of the boolean type
- Readability: When using
boolThe type makes the program's intent clearer; compared with usinginttypes to represent true or false,boolthe type clearly indicates that the variable is only used to represent a boolean value. - Standardization:
It provides a standard way to handle boolean values, avoiding the complexity of using macros or integer types to represent them. - Type Safety: Together with
intUnlike the type,boolthe type is dedicated to logical judgments, reducing the problem of type mismatches.