gets()
函数已从 C 语言中删除。标准中没有这样的功能。
但我编译了以下代码:
#include <stdio.h>
int main (void)
{
(void) gets (NULL);
}
使用
gcc -std=c11 -pedantic-errors -Wall -Wextra
它编译时没有给出任何错误或警告。同样,
#include <stdio.h>
int gets;
int main (void)
{}
不会编译(错误:'gets' 重新声明为不同类型的符号)。
在标准 4. Conformance §6 中我们可以读到:
A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program
鉴于上述情况,我认为 gcc 不符合标准,即使在迂腐模式下也是如此。是否有一个原因?这是故意的还是错误?
海湾合作委员会版本 4.9.1。
编辑:
gcc --version
gcc (x86_64-win32-seh-rev1, Built by MinGW-W64 project) 4.9.1
最佳答案
gcc 只是编译器,不是整个实现。
在我的系统(Linux Mint 17.3、gcc 4.8.4、GNU libc 2.19)上,我得到:
$ 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);
^
要正确诊断错误,实现 需要符合要求。这意味着编译器(它从一开始就从未提供 gets
)和库。
您使用的库仍然提供 gets
函数。因此,整个实现(由编译器 gcc、库和其他一些部分组成)不符合 C11。
底线:这不是 gcc 问题,gcc 对此无能为力。 (好吧,它可以为 gets
发出一个特例诊断,但随后它必须确定它不是对具有相同功能的用户定义函数的有效调用名。)
关于c - 为什么我可以在 gcc -std=c11 中使用 gets()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30619780/