How to Read the Error Line
GCC errors all look alike. Start with the line from the top of this post, bad1.c:4:13: error: implicit declaration of function 'square'. bad1.c is the file. 4 is the line. 13 is the column where things go wrong. The word error means the build stops. A warning wouldn't stop it. The part in square brackets, [-Wimplicit-function-declaration], names the check that fired, so it's a handy thing to search for. Lines that start with note: are hints. For this error, they're the lines that hand you the fix.
Functions From Your Own Files
Functions you write in another file follow the same rule. Put the declaration in a header, and make every file that calls the function include it. Take the task manager from How to Build Real-World Projects in C. There, tasks.h declares the functions and main.c starts with #include "tasks.h". I deleted that one line and compiled main.c. The first error was unknown type name 'TaskList'. After it came implicit declaration of function 'tasklist_init', and the same error for the other functions. Quotes go around your own headers. Angle brackets go around standard ones.
The Short Answer
implicit declaration of function means you called a function the compiler hasn't seen yet. C reads your file from top to bottom. So the function has to be declared before the line that uses it. There are three fixes: add the right #include, add a prototype above main, or correct a typo in the function name.
bad1.c: In function 'main':
bad1.c:4:13: error: implicit declaration of function 'square' [-Wimplicit-function-declaration]
4 | int r = square(4);
| ^~~~~~
Read the message from the end. It names the function the compiler couldn't find ('square') and the line of the call (bad1.c:4). That's the whole clue. Below are the three causes, in the order beginners usually run into them.
What the Error Really Means
To check a function call, the compiler needs the return type and the argument types. A declaration (you'll also hear "prototype") gives it both. Without one, very old C just guessed that the function returns int and moved on. That guess is the "implicit declaration". It hid real bugs. You could pass the wrong argument types or assume the wrong return type, and the program still built.
Modern compilers don't allow the guess anymore. With gcc 16.2, which I used for every example here, it's a hard error and you get no program. Older gcc versions only printed a warning and kept going. That's why older tutorials call it a "warning". Treat it as an error either way.
Cause 1: You Forgot the #include
This is the most common cause. Standard functions live in headers, and you have to include the one that declares the function you're using. Here strlen is called without <string.h>:
#include <stdio.h>
int main(void) {
char s[] = "hello";
printf("%zu\n", strlen(s));
return 0;
}
bad2.c:5:21: error: implicit declaration of function 'strlen' [-Wimplicit-function-declaration]
bad2.c:2:1: note: include '<string.h>' or provide a declaration of 'strlen'
gcc even names the header in the note: line. Add it and the program builds:
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "hello";
printf("%zu\n", strlen(s));
return 0;
}
5
sqrt and <math.h> work the same way. Leave out the header and gcc reports implicit declaration of function 'sqrt'. Add #include <math.h> and sqrt(16.0) prints 4.0. Here's which header goes with which function:
printf,scanf,fgets,fopen:<stdio.h>strlen,strcpy,strcmp,memcpy:<string.h>malloc,free,atoi,exit:<stdlib.h>sqrt,pow,sin:<math.h>isalpha,toupper:<ctype.h>
Say you use sqrt or pow and then the linker complains about an undefined reference. Add -lm to your gcc command. That's a different error, from the linker, and whether you need it depends on your platform.
Cause 2: You Defined the Function After main
Your own functions obey the same rule. In this program square is defined below main. When main calls it, the compiler has never heard of it:
#include <stdio.h>
int main(void) {
int r = square(4);
printf("%d\n", r);
return 0;
}
int square(int n) {
return n * n;
}
You've got two ways out. The usual one is a prototype above main. It keeps main at the top of the file, where readers look for it:
#include <stdio.h>
int square(int n);
int main(void) {
int r = square(4);
printf("%d\n", r);
return 0;
}
int square(int n) {
return n * n;
}
16
The prototype int square(int n); is just the signature, with a semicolon where the body would be. The other way is to move the whole square function above main. Either one works. In bigger projects, prototypes go in a .h file that you include. The standard headers do exactly that.
Cause 3: A Typo in the Function Name
Misspell a name and the compiler sees a function it has never met. It's the same error with a different cause:
#include <stdio.h>
int main(void) {
prinft("hi\n");
return 0;
}
typo.c:4:5: error: implicit declaration of function 'prinft'; did you mean 'printf'? [-Wimplicit-function-declaration]
This time gcc even suggests the right spelling. Capitals count, too. Printf, String_length and strlen are three different names to C.
A Quick Way to Tell Which Cause You Have
Start with the name in the error message. Does it match what you meant to type?
Standard function? Read the
note:line under the error. gcc usually names the header there, so add that#include.Your own function? Find its definition. Sitting below the call means you need a prototype above
main.Nothing fits? Make sure the defining file is on your build line, like
gcc main.c utils.c -o app.
Compile With Warnings On
Build with gcc -Wall -Wextra every time. A lot of beginner mistakes show up as a compiler message long before they turn into a wrong answer or a crash. This error is one of them. The same habit catches the bugs in a C program that prints a garbage value and in a scanf call that is missing its &.
Maybe your program builds but still skips input. That's a separate problem, and it's covered in why your C program skips the second scanf. If you're just starting out, the Introduction to C programming guide walks through includes, main and compiling from scratch.
The Checklist
You spelled the function name, capitals included, exactly like its declaration.
You included the header for each standard function you call (
stdio.h,string.h,stdlib.h,math.h).Each of your own functions is defined or prototyped before the first call to it.
Any
.cfile that defines a function you call is part of the gcc command.You build with
-Wall -Wextra, and you deal with the first error before the rest.
Your logic isn't the problem. The compiler just wants an introduction to the function before you use it, and one include or one prototype line normally does that.
Download the code: broken and fixed versions of four of these causes (a missing include, a function used before it is declared, a typo, and a missing <math.h>) are on GitHub in C-Programing/Troubleshooting/implicit-declaration. Compile one file at a time, for example gcc -Wall -Wextra bug.c -o bug.exe. Getting undefined reference instead? That one comes from the linker. See gcc: Undefined Reference to WinMain (Fix on Windows). If your program builds but then crashes, see Segmentation Fault in C: Find the Exact Line.
Originally published on my blog. I post new C, Python and AI-agent guides there regularly.
Top comments (0)