/errors/c/implicit-declaration-of-function
C error: implicit declaration of function 'foo' [-Wimplicit-function-declaration]
- tool
- C
- exit code
- 1
- versions
- GCC 14+ (error by default), Clang 16+ (error by default), older compilers (warning)
- os
- linux, macos, windows
implicit declaration of function 'foo' means the C compiler reached a call to foo without having seen a declaration of it. C99 removed implicit declarations, and current compilers treat them as errors: GCC 14 reports error: implicit declaration of function 'foo' [-Wimplicit-function-declaration], and Clang 16+ reports error: call to undeclared function 'foo'; ISO C99 and later do not support implicit function declarations. Include the header that declares the function, or add a prototype before the call.
Example
#include <stdio.h>
int main(void) {
printf("%zu\n", strlen("hello")); /* strlen is declared in <string.h> */
printf("%d\n", square(4)); /* square is defined below main */
return 0;
}
int square(int n) { return n * n; }Compiling with gcc main.c reports both calls. Older GCC versions printed a warning and assumed the function returned int, which silently breaks functions that return pointers, long or double.
Fix
#include <stdio.h>
#include <string.h> /* declares strlen */
int square(int n); /* prototype before the first call */
int main(void) {
printf("%zu\n", strlen("hello"));
printf("%d\n", square(4));
return 0;
}
int square(int n) { return n * n; }Which header?
The man page of each function lists its header under SYNOPSIS (man 3 strlen). Common ones:
| Functions | Header |
|---|---|
| strlen, strcpy, memcpy, memset | <string.h> |
| malloc, free, exit, atoi | <stdlib.h> |
| sleep, read, write, close, getpid | <unistd.h> (POSIX) |
| isdigit, toupper | <ctype.h> |
| sqrt, pow | <math.h> (link with -lm on Linux) |