我想知道它的缺点scanf().
在许多站点中,我已经读过使用scanf可能导致缓冲区溢出.这是什么原因?还有其他缺点scanf吗?
如果我5 5在终端输入,按回车键,然后再次按回车键,我想退出循环.
int readCoefficents(double complex *c){
int i = 0;
double real;
double img;
while(scanf("%f %f", &real, &img) == 2)
c[i++] = real + img * I;
c[i++] = 1 + 0*I; // most significant coefficient is assumed to be 1
return i;
}
Run Code Online (Sandbox Code Playgroud)
显然,该代码并没有为我完成工作(是的,我知道有一个缓冲区溢出等待发生).
scanf除非我输入一个字母(或一些非数字,而不是空白字符串),否则不会退出.读取空行后如何让scanf退出?
#include <stdio.h>
int main()
{
char temp[1024];
if(getchar() != 'y')
{
printf("no options\n");
return 1;
}
scanf(temp, "%s");
printf("%s", temp);
}
Run Code Online (Sandbox Code Playgroud)
我得到如下代码片段.我只想要两次来自用户的输入.但第一个输入工作,但第二个直接跳过和printf("%s", temp);打印的意外字符.我怎样才能解决问题.. thanx