How to fix C error 'false undeclared'

Problem:

You have C code like

example.txt
bool mybool = false;
```c {filename="main.c"}

but when you try to compile it you see an error message like

```plaintext {filename="false_undeclared_output.txt"}
main.c: In function ‘main’:
main.c:2:5: error: unknown type name ‘bool’; did you mean ‘_Bool’?
     bool mybool = false;
     ^~~~
     _Bool
main.c:2:19: error: ‘false’ undeclared (first use in this function)
     bool mybool = false;
                   ^~~~~
main.c:2:19: note: each undeclared identifier is reported only once for each function it appears in

Solution

Add

main_fixed.c
#include <stdbool.h>

at the top of the source file where the error occured.

The reason for this error is that bool is not a standard type in C (like int or char) but only in stdbool.h. Also, true and false are declared in stdbool.h.


Check out similar posts by category: C/C++, GCC Errors