69,369
社区成员
发帖
与我相关
我的任务
分享
fgets(buf,len,stdin)
char buf[5];
gets(buf);
如果输入的字符串时hello,那么就会越界了。但是gets不会提示也不会截断输入的hello字符串;
建议使用fgets,如上面的代码,用fgets替换,
char buf[5];
fgets(buf, 5, stdin);
则输入hello,它会将hello截断,只将hell复制到buf里,并加上'\0'
建议编写一个小程序试一下,fgets也有另一个需要注意的,建议测试的能够发现。int Getchar(charchar *A,int min,int max)//输入的字符串长度在[min,max] <闭区间> 之间时 函数结束 返回字符串A的长度
{
int B,C;
do{
A[max]=B=C=0;
while((A[B++]=getchar())!='\n'&&B<max);
if(A[B-1]!='\n')while(getchar()!='\n'&&++C);
else A[--B]=0;
if(C||B&&B<min)
printf("您录入的字符串长度:%d字节\n只录入(%d--%d)个字节!\n",B+C,min,max);
}while(C||B<min);
return B;
}