c语言gets怎么只执行了一个

只执行了第一个gets

gets_s()是C11标准,由于C11标准还没有完全普及很多编译器还不支持C11标准。我的编译器还不支持C11标准,在这我们就用fgets代替gets。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char buf[10];
gets(buf);
printf("%s",buf);
printf("hello\n");
return 0;
}
1
2
3
4
5
6
7
8
9
10

从上图我们看出gets没有将\n符号写到缓冲区,我们打印的时候没有出现换行

从上图我们看出gets函数输入的数据过多,buffer空间不够,多出的数据将会写入堆栈中造成不可预料的错误。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char buf[10];
fgets(buf,10,stdin);// stdin 表示中断输入
printf("%s",buf);
printf("hello\n");
return 0;
}
1
2
3
4
5
6
7
8
9
10
结果

从上图我们看出,fgets读到\n结束符结束,且当前的数据小于n-1(9),且fgets将\n也写入的缓冲区。

如上图,当输出的数据大于n-1(9)的时候,发现\n符号没有写到缓冲区,且最后一个数据也么有写到缓冲。原来我们对于fgets,必须指定缓存的长度n。此函数一直读到下一个新行符为止,但是不超过n-1个字符,读入的字符被送入缓存。该缓存以null字符结尾。如若该行,包括最后一个新行符的字符数超过n-1,则只返回一个不完整的行,而且缓存总是以null字符结尾。对fgets的下一次调用会继续读该行。
---------------------
温馨提示:答案为网友推荐,仅供参考
相似回答