C implicit declaration of function
warning: implicit declaration of function ‘strlen’
This warning means you called a function whose name the compiler has not seen yet. A C compiler reads the file from top to bottom, so the function's declaration has to come above the line that calls it. The declaration is either in a header you #include or one you write yourself. The name in single quotes is the function in question, so start by working out whether it is a standard function, a typo, or a function you wrote.
Cause 1. You left out an #include
The declaration of strlen is in <string.h>. Without that header, the compiler does not know what strlen is. GCC also tells you which header to add, as in note: include '<string.h>'. The output below shows two warnings, and both point to the same mistake on the same line.
This example only gives warnings and still runs. Even so, don't leave it that way. Without a declaration, the compiler can't check the types of the arguments and the return value, so with other functions you can get wrong values.
Wrong code
#include <stdio.h> int main(void) { char s[] = "hello"; printf("%d\n", (int)strlen(s)); return 0;}main.c: In function ‘main’:
main.c:5:25: warning: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration]
5 | printf("%d\n", (int)strlen(s));
| ^~~~~~
main.c:2:1: note: include ‘<string.h>’ or provide a declaration of ‘strlen’
1 | #include <stdio.h>
+++ |+#include <string.h>
2 |
main.c:5:25: warning: incompatible implicit declaration of built-in function ‘strlen’ [-Wbuiltin-declaration-mismatch]
5 | printf("%d\n", (int)strlen(s));
| ^~~~~~
main.c:5:25: note: include ‘<string.h>’ or provide a declaration of ‘strlen’
5Fixed code
#include the header that contains the function at the top of the file.
#include <stdio.h>#include <string.h> int main(void) { char s[] = "hello"; printf("%d\n", (int)strlen(s)); return 0;}5
Headers for common functions: printf scanf are in <stdio.h>, strlen strcpy strcmp in <string.h>, malloc free abs atoi in <stdlib.h>, sqrt pow in <math.h>, and isdigit toupper in <ctype.h>.
Cause 2. You misspelled the function name
If you type prinft for printf, the compiler sees a function it has never met. When there is a similar name, it asks did you mean 'printf'?.
This time it does not stop at a warning. No function called prinft exists anywhere, so right afterwards, in the link step, you get an undefined reference to error and no executable is made. The /tmp/cc....o on that line is the name of a temporary file the compiler made, so it changes every time you compile.
Wrong code
#include <stdio.h> int main(void) { prinft("hello\n"); return 0;}main.c: In function ‘main’:
main.c:4:5: warning: implicit declaration of function ‘prinft’; did you mean ‘printf’? [-Wimplicit-function-declaration]
4 | prinft("hello\n");
| ^~~~~~
| printf
/usr/bin/ld: /tmp/ccRp4kfl.o: in function `main':
main.c:(.text.startup+0x10): undefined reference to `prinft'
collect2: error: ld returned 1 exit statusFixed code
Fix the spelling and the upper and lower case of the name. C treats Printf and printf as different names too.
Cause 3. Your own function is below main
When the compiler reads the line in main that calls add, it has not yet seen add, which is further down. So it warns that this is a function it has not seen.
Wrong code
#include <stdio.h> int main(void) { printf("%d\n", add(3, 4)); return 0;} int add(int a, int b) { return a + b;}main.c: In function ‘main’:
main.c:4:20: warning: implicit declaration of function ‘add’ [-Wimplicit-function-declaration]
4 | printf("%d\n", add(3, 4));
| ^~~
7Fixed code
Move the whole function above main, or write a one-line declaration (int add(int a, int b);) above main. A declaration is the first line of the function with a ; added.
#include <stdio.h> int add(int a, int b); int main(void) { printf("%d\n", add(3, 4)); return 0;} int add(int a, int b) { return a + b;}7
Without a declaration, the compiler assumes the function returns int. That is why add, which returns int, ends with just a warning. If the function returned double, the guess would be wrong and you would also get a conflicting types for error.
What to check when you see this error
- Look at the function name in single quotes in the warning. Are the spelling and the upper and lower case right?
- If it is a standard function,
#includethe header named on thenote: include ...line just below. - If it is your own function, check that the function or its declaration comes above the line that calls it.
- Fix it even if it is only a warning and the program runs. From GCC 14 on, this is an error, not a warning.
- The message differs between compilers. clang says
call to undeclared function, and Visual Studio sayswarning C4013.