隐式声明“获取" [英] Implicit declaration of 'gets'
问题描述
我了解到隐式声明"通常意味着必须在调用函数之前将其置于程序的顶部,否则我需要声明原型.
但是,gets
应该在stdio.h
文件中(该文件已包括在内).
有什么办法可以解决这个问题?
I understand that an 'implicit declaration' usually means that the function must be placed at the top of the program before calling it or that I need to declare the prototype.
However, gets
should be in the stdio.h
files (which I have included).
Is there any way to fix this?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char ch, file_name[25];
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen(file_name,"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
}
推荐答案
您是对的,如果包含适当的标头,则不应收到隐式声明警告.
You are right that if you include proper headers, you shouldn't get the implicit declaration warning.
但是,功能gets()
已从C11标准中删除 .这意味着<stdio.h>
中不再有gets()
的原型. gets()
以前用于<stdio.h>
.
However, the function gets()
has been removed from C11 standard. That means there's no longer a prototype for gets()
in <stdio.h>
. gets()
used to be in <stdio.h>
.
删除gets()
的原因是众所周知的:它不能防止缓冲区溢出.因此,您永远不要使用gets()
并使用 fgets()
代替,并注意结尾的换行符(如果有).
The reason for the removal of gets()
is quite well known: It can't protect against the buffer overrun. As such, you should never use gets()
and use fgets()
instead and take care of the trailing newline, if any.
这篇关于隐式声明“获取"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!