How to fix C error 'false undeclared'
Problem:
You have C code like
bool mybool = false;
but when you try to compile it you see an error message like
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
#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
.