Tutorial by Examples

C99 Using the system header file stdbool.h allows you to use bool as a Boolean data type. true evaluates to 1 and false evaluates to 0. #include <stdio.h> #include <stdbool.h> int main(void) { bool x = true; /* equivalent to bool x = 1; */ bool y = false; /* equivalent t...
C of all versions, will effectively treat any integer value other than 0 as true for comparison operators and the integer value 0 as false. If you don't have _Bool or bool as of C99 available, you could simulate a Boolean data type in C using #define macros, and you might still find such things in...
C99 Added in the C standard version C99, _Bool is also a native C data type. It is capable of holding the values 0 (for false) and 1 (for true). #include <stdio.h> int main(void) { _Bool x = 1; _Bool y = 0; if(x) /* Equivalent to if (x == 1) */ { puts("T...
All integers or pointers can be used in an expression that is interpreted as "truth value". int main(int argc, char* argv[]) { if (argc % 4) { puts("arguments number is not divisible by 4"); } else { puts("argument number is divisible by 4"); } ... ...
Considering that most debuggers are not aware of #define macros, but can check enum constants, it may be desirable to do something like this: #if __STDC_VERSION__ < 199900L typedef enum { false, true } bool; /* Modern C code might expect these to be macros. */ # ifndef bool # define bool bo...

Page 1 of 1