13

The gets() function has been removed from the C language. No such function exists in the standard.

Yet I compile the following code:

#include <stdio.h>

int main (void)
{
  (void) gets (NULL);
}

using

gcc -std=c11 -pedantic-errors -Wall -Wextra

and it compiles without giving any errors or warnings. Similarly,

#include <stdio.h>

int gets;

int main (void)
{}

will not compile (error: 'gets' redeclared as different kind of symbol).

In the standard 4. Conformance §6 we can read:

A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program

Given the above I don't think gcc is standard-compliant, even in pedantic mode. Is there a reason for this? Is this intentional or is it a bug?

GCC version 4.9.1.

Edit:

gcc --version
gcc (x86_64-win32-seh-rev1, Built by MinGW-W64 project) 4.9.1
14
  • 1
    GCC 4.8.2 here. In pedantic mode the first snippet will not compiler because gets is never defined. In non-pedantic mode I get warning: the `gets' function is dangerous and should not be used.. The second snippet is not an issue, because nothing prevents you from declaring a symbol called gets. For instance, int printf; is perfectly legal. Am I missing the point of the question? Commented Jun 3, 2015 at 12:18
  • 2
    I'm for a bug specific to gcc 4.9.1 as version 4.8.2 and > 4.9.2 do fail. Commented Jun 3, 2015 at 12:23
  • 11
    MinGW uses Microsoft's standard library, which does not even support C99 properly. Commented Jun 3, 2015 at 12:32
  • 2
    @haccks: No, gcc has never supported gets -- or fgets for that matter. If gets is supported by a given implementation, it's implemented by the library, not by the compiler. Commented May 27, 2016 at 19:28
  • 2
    @haccks: Yes and no. In the <stdio.h> header provided by the GNU C library, gets is still declared, but the declaration is surrounded by #if !defined __USE_ISOC1 ... #endif. The implementation is still there, but in a way that permits user code to define a function with the same name. Commented May 27, 2016 at 19:42

3 Answers 3

12
+50

gcc is just the compiler, not the entire implementation.

On my system (Linux Mint 17.3, gcc 4.8.4, GNU libc 2.19), I get:

$ gcc -std=c11 -pedantic-errors -Wall -Wextra -c c.c
c.c: In function ‘main’:
c.c:5:3: error: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
   (void) gets (NULL);
   ^

To correctly diagnose the error, the implementation needs to be conforming. That means both the compiler (which never provided gets in the first place) and the library.

You're using a library that still provides the gets function. Because of that the implementation as a whole (which consists of the compiler gcc, the library, and a few other pieces) does not conform to C11.

Bottom line: This is not a gcc issue, and there's not much that gcc can do about it. (Well, it could issue a special-case diagnostic for gets, but then it would have to determine that it's not a valid call to a user-defined function with the same name.)

Sign up to request clarification or add additional context in comments.

7 Comments

Though C standard library droped gets, GNU C library still includes this function.
Copying my response to your other comment: @haccks: Yes and no. In the <stdio.h> header provided by the GNU C library, gets is still declared, but the declaration is surrounded by #if !defined __USE_ISOC1 ... #endif. The implementation is still there, but in a way that permits user code to define a function with the same name.
OK. Do you consider this as a bug?
@haccks: It's a failure of this particular implementation (MinGW) to conform to the C11 standard. (See also MinGW's problems with long double, something that applies to C90, C99, and C11.) Sure, I'd say it's a bug. (But it's an easily avoidable one -- don't use gets, even if your compiler fails to warn you about it.)
Thanks for correcting me. I am glad you came to this post almost after an year (little bit surprised!!) to share this information.
|
1

The key line of your code is:

#include <stdio.h>

Did you update your system's C library and headers? They're also part of the C implementation, along with the compiler.

2 Comments

I did run mingw-get update and mingw-get upgrade, same error afterwards.
As Andrew mentioned, the key line is the #include <stdio.h>. You should find and analyze the stdio.h file that is used by gcc to perform the compilation. That file probably still has the gets function defined.
1

update this may not be an answer to the question, I try to make it informational.

I happened to find that gcc mentioned gets is not following C11 standard for some library issue glibc 2.16.

See gcc supporting status of C11: https://gcc.gnu.org/wiki/C11Status croped from the above link

But I cannot find the definition of "library issue" and current status for other versions of glibc.

So I tried on my machine ubuntu16.04 with gcc version 5.3.1 20160413, glibc version Ubuntu GLIBC 2.23 We can get enough warning on compile time, but it's still OK to execute the output object file for "Backwards compatibility".

warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
warning: the `gets' function is dangerous and should not be used.

2 Comments

This is misleading. gcc didn't remove gets because gcc never provided gets in the first place. It's provided by the library, not by the compiler (which why that status page refers to it as a "library issue").
@Keith Thompson. Thanks for the answer! That's amazing clear and helpful :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.