Using define
suggest changeC 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 legacy code.
#include <stdio.h> #define bool int #define true 1 #define false 0 int main(void) { bool x = true; /* Equivalent to int x = 1; */ bool y = false; /* Equivalent to int y = 0; */ if (x) /* Functionally equivalent to if (x != 0) or if (x != false) */ { puts("This will print!"); } if (!y) /* Functionally equivalent to if (y == 0) or if (y == false) */ { puts("This will also print!"); } }
Don’t introduce this in new code since the definition of these macros might clash with modern uses of <stdbool.h>
.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents