You're getting a message like "implicit declaration of gets is not allowed" for the same reason you'd get the message "implicit declaration of my_undeclared_function is not allowed" if you tried to call my_undeclared_function
. These days, gets
is not a standard C library function.
To explain in more detail: in modern C, if you write
int main()
{
f();
}
int f()
{
return 0;
}
you will typically get a message like "implicit declaration of function 'f' is invalid in C99". Once upon a time, if you called a function the compiler hadn't heard of (yet), it assumed it was going to be a function returning int
, but that assumption was removed in C99.
On the other hand, if you write
#include <stdio.h>
int main()
{
char line[100];
printf("type something:\n");
fgets(line, sizeof(line), stdin);
printf("you typed: %s", line);
return 0;
}
you will not get that message about the functions printf
and fgets
. These are functions that the compiler has heard of: their declarations are in <stdio.h>
.
But if you write
#include <stdio.h>
int main()
{
char line[100];
printf("type something:\n");
gets(line);
printf("you typed: %s", line);
return 0;
}
you will get the message again, about function gets()
, because gets
is not a function the compiler has heard of. It is not a standard function; it is not declared in <stdio.h>
.
Now, once upon a time, gets
was a standard function and it was declared in <stdio.h>
, but it was a spectacularly ill-conceived and dangerous function, so it has been removed from the standard. The book you read recommending it is quite out-of-date.