How to fix C error 'true undeclared'
Problem:
You have C code like
bool mybool = true;
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 = true;
^~~~
_Bool
main.c:2:19: error: ‘true’ undeclared (first use in this function)
bool mybool = true;
^~~~
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
.