How to fix GCC error 'implicit declaration of function printf'


You have C code like


but when you try to compile it you see a warning like

main.c: In function ‘main’:
main.c:2:5: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
main.c:2:5: warning: incompatible implicit declaration of built-in function ‘printf’
main.c:2:5: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’



#include <stdio.h>

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

Note that this warning message is just a warning and if you use printf correctly, your program will work even without #include <stdio.h>. However, incorrect usage of printf and similar functions will lead to hard-to-debug errors, so I recommend to add #include <stdio.h> in any case, even if it works without.