Boolean in C
Before C99 the boolean type did not exist in C, so you made your own with preprocessor directives. C99 added <stdbool.h>, and C23 makes bool a keyword. The example below is the old way and is compiled as C17: in the default C23 mode gcc rejects typedef char bool; because bool is already a keyword.
#include <stdio.h>
#ifndef __cplusplus
typedef char bool;
#define true 1
#define false 0
#endif
Here is the whole program, with the type used in main:
#include <stdio.h>
#ifndef __cplusplus
typedef char bool;
#define true 1
#define false 0
#endif
int main(void)
{
bool flag = true;
printf("flag = %d\n", flag);
flag = false;
printf("flag = %d\n", flag);
return 0;
}
gcc -std=c17 -Wall -Wextra -Wpedantic bool.c -o bool
./bool
flag = 1 flag = 0
Article author: Andrei Olegovich