Why can I use gets() in gcc -std=c11?

送分小仙女□ 提交于 2019-12-04 17:17:38

问题


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

回答1:


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.)




回答2:


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.




回答3:


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

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.


来源:https://stackoverflow.com/questions/30619780/why-can-i-use-gets-in-gcc-std-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!