Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

clang

gets

c99

cs50

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.

like image 924
Mr. Anderson Avatar asked Jan 29 '23 12:01

Mr. Anderson


1 Answers

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.

like image 185
John Bode Avatar answered Feb 05 '23 02:02

John Bode