gets( )用法?
#include <stdio.h>
int main()
{
char a[50];
gets(a);
puts(a);
return 0;
}
z@z:~$ gcc a.c
/tmp/ccJlrK3u.o: In function `main':
a.c:(.text+0x1d): warning: the `gets' function is dangerous and should not be used.
为什么?
[解决办法]
我在gcc下编译通过,至于警告应该是说gets是不安全的,推荐用fgets等
[解决办法]
用fgets。
gets无法控制缓冲区大小,输入行过长就会内存溢出。
[解决办法]
程序没错,仅是警告,因为gets是不安全的。
用fgets 或者gets_s(如果你的编译器支持c11的话)。
fgets(a, sizeof(a), stdin)
gets_s(a, 50)