为什么我可以在gcc -std = c11中使用gets()? [英] Why can I use gets() in gcc -std=c11?

查看:276
本文介绍了为什么我可以在gcc -std = c11中使用gets()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经从C语言中删除 gets()函数。标准中不存在这样的函数。然后我编译下面的代码:

  #include< stdio.h> 

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

使用

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




  #include< stdio.h> 

int获取;
$ b $ int main(void)
{}

不会编译(错误:'gets'被重新声明为不同类型的符号)。



在标准4.一致性§6中,我们可以读取:


一致性实现可能具有扩展(包括额外的
库函数),前提是它们不会改变任何
严格符合程序的行为


鉴于上述情况,我认为即使在迂回模式下,gcc也不符合标准。是否有一个原因?这是故意的还是一个bug?



GCC版本4.9.1。



编辑:

  gcc --version 
gcc(x86_64-win32-seh-rev1,由MinGW-W64项目构建)4.9.1


解决方案

gcc只是编译器,而不是整个实现。 / p>

在我的系统上(Linux Mint 17.3,gcc 4.8.4,GNU libc 2.19),我得到:

 cc:在函数'main'中:
cc:5:3:error:隐式声明函数'gets'[-Wimplicit-function-declaration]
(void)gets(NULL);

要正确诊断错误,实现需要符合。这意味着编译器(从未提供首先获取)和库。



你是使用仍提供的库获取函数。因为实现作为一个整体(由编译器gcc,库和其他一些组件组成)不符合C11。



底线:这不是gcc问题,gcc可以做的并不多。 (好吧,它可以为获取发出特殊情况诊断,但是它必须确定它不是对用户的有效调用 - 具有相同名称的定义函数。)


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

解决方案

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

这篇关于为什么我可以在gcc -std = c11中使用gets()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆