How to fix C error ‘NULL undeclared’


You have C code like

char* ptr = NULL;

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

main.c: In function ‘main’:
main.c:3:17: error: ‘NULL’ undeclared (first use in this function)
     void* ptr = NULL;
main.c:3:17: note: each undeclared identifier is reported only once for each function it appears in



#include <stddef.h>

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

The reason for this error is that NULL is not defined in C itself but only in stddef.h.