_Bool
|
data type that is not available in other C derivates (like C++)
They also define some symbolic names to represent the Boolean constants true and false in a header file to make a C program "feels like" it is using boolean values
Take a look at:
/usr/lib/gcc/x86_64-linux-gnu/7/include/stdbool.h |
You will see these definitions that simulate Boolean constants:
#define bool _Bool #define true 1 #define false 0 |
You can include this header file in your C program using:
#include <stdbool.h> // <...> means: search in C's include files
|
|
|
0 = false Any other value = true |
Example: here are 4 if-statements using variable numerical values in C:
#include <stdio.h>
#include <stdbool.h>
int main( int argc, char* argv[] )
{
printf("true = %d\n", true);
printf("false = %d\n\n", false);
if ( 1 )
printf("value 1 is true\n");
else
printf("value 1 is false\n");
if ( 0 )
printf("value 0 is true\n");
else
printf("value 0 is false\n");
if ( 0.1 )
printf("value 0.1 is true\n");
else
printf("value 0.1 is false\n");
if ( 0.0 )
printf("value 0.0 is true\n");
else
printf("value 0.0 is false\n");
}
|
Output:
true = 1 false = 0 value 1 is true value 0 is false value 0.1 is true value 0.0 is false |
Confirming the fact that: 0 ≡ false and any value ≠ 0 ≡ true
How to run the program:
|