/

How to Fix the Implicitly Declaring Library Function Warning in C

How to Fix the Implicitly Declaring Library Function Warning in C

In the process of compiling a C program, you may encounter a warning issued by the compiler that looks like this:

1
2
3
hello.c:6:3: warning: implicitly declaring library function 'printf' with type 'int (const char *, ...)' [-Wimplicit-function-declaration]
printf("Name length: %u", length);
^

or

1
2
3
hello.c:5:16: warning: implicitly declaring library function 'strlen' with type 'unsigned long (const char *)' [-Wimplicit-function-declaration]
int length = strlen(name);
^

This warning arises because you have used a function from the standard library without including the appropriate header file.

Fortunately, the compiler provides a helpful suggestion:

1
hello.c:5:16: note: include the header <string.h> or explicitly provide a declaration for 'strlen'

This pointer guides you in the right direction.

To resolve this issue, simply add the following line at the top of your C file:

1
#include <stdio.h>

This inclusion will rectify the problem and enable successful compilation.