Boolean Type in C Language
Boolean Type in C language 1. Basic Knowledge
First, bool true false is the keyword in C ++. By default, these characters are not supported in C!
Ii. Details
No Boolean-related content defined in the C89 (ansi c) Standard
However, a New Keyword _ Bool is defined in the C99 standard, and a new header file <stdbool. h> standardizes Boolean operations to facilitate calls by programmers!
The following content is defined in the <stdbool. h> header file:
# Define bool_Bool
# Define false0
# Define true1
In this way, the compiler that supports the C99 standard only needs to include stdbool. h, so it is very convenient to use the commonly used bool false true to operate the boolean type.
However, some compilers may not support the C99 standard! That is, you cannot use the keyword _ Bool and may not be able to contain the stdbool. h header file. Therefore, you can define some Boolean operations:
# Define bool int
# Define true 1
# Define false 0
Iii. analysis summary
That is, the _ Bool and stdbool header files cannot be used in compilers that do not support the C99 standard. h. You can use _ Bool unconditionally in a compiler that supports the C99 standard. Because it is a keyword, its value can only be 0 or 1. If you want to use bool, false, true indicates that the header file stdbool must be included. h
Iv. instance test code
Use the following program to test in VS2015:
# Include <stdio. h>
Int main (void)
{
_ Bool a =-112;
Printf ("% d \ n", );
Getchar ();
Return 0;
}
Program output 1
That is, if 0 is assigned to a, 0 is output. If a value of _ Bool type a is assigned any other value, it will be converted to 1, that is, if it is not 0, it will be changed to 1!
The following programs are compiled in VS2015, indicating that VS2015 supports the C99 standard.
# Include <stdio. h>
# Include <stdbool. h>
Int main (void)
{
_ Bool a = 1;
Bool B = true;
Bool c = false;
If (a = true)
{
Printf ("% d \ n", );
Printf ("% d \ n", B );
Printf ("% d \ n", c );
}
Getchar ();
Return 0;
}