Why is implicit declaration of gets() not allowed in C99?

Mr. Anderson picture Mr. Anderson · Mar 13, 2018 · Viewed 10.4k times · Source

I am starting to learn programming in C language the book I am refering to code shows some source code with gets() and my IDLE recognises it as well. But still while compiling it, my compiler doesn't agree with it.

Can anyone help me out? I am using gets() in the main function and using clang as the compiler.

Answer

John Bode picture John Bode · Mar 13, 2018

Expanding on my comment:

First, never use gets(), for any reason, even in toy code. If you see it in an example program, ignore the example and move on. It was deprecated in C99 and removed from the standard library completely in C2011 because it was a major security hole. The heartburn caused by that one function was worth breaking 30-some-odd years of legacy code.

Second, under C89 and earlier, if the compiler saw a function call before it saw a declaration or definition, it would assume the function returned int - IOW, the function had an implicit declaration of int. If you had a function definition later in the same translation unit, and the function returned an int, you were fine:

int foo( void )
{
  int x = bar(); // bar *implicitly* declared to return int
}

int bar( void ) // implicit declaration matches definition, we're good
{
  return some_integer_value;
}

However, if bar did not return an int, you'd get an error because the implicit int declaration did not match up with the non-int definition.

gets() returns char *, not int, so an implicit declaration of gets is incorrect regardless.

C99 removed implicit int declarations entirely - since then, all functions must be declared or defined before they are called.

EDIT

The most likely reason you're getting that implicit declaration error is that your compiler is recent enough that it no longer has a declaration for gets in stdio.h.