【发布时间】:2015-08-20 21:45:37
【问题描述】:
使用fflush(stdin)有什么区别
和flushstdin()?我知道的唯一区别是我需要在使用 flushstdin() 之前写下那个 void 的东西,但我不知道为什么。
void flushstdin()
{
int c;
while((c = getchar()) != '\n' && c != EOF);
}
int main () {
float a, b, c;
float s=0, ar1=0, ar2=0;
printf("Inform value of side A");
while(scanf("%f",&a) != 1 || a <= 0){
printf("Invalid value.\n");
flushstdin();
}
}
和
int main(){
float a,b,c,s=0;
printf("Inform value of side A.");
while(scanf("%f",&a) != 1 || a<=0){
printf("Invalid value.\n");
fflush(stdin);
}
}
我是初学者!哪个代码最好?或者他们是平等的?
【问题讨论】:
-
你说的我需要在使用flushstdin()之前写下那个空的东西是什么意思?
-
@RSahu this: void flushstdin() { int c; while((c = getchar()) != '\n' && c != EOF);
-
请告诉你“写那些空洞的东西”的人。你还应该告诉她这个“空的东西”是一个 function 并且没有“写”它(在文件的上下文中“写东西”有一定的语义),但是 define (这里也是declare)和稍后的call。他真的应该把条款弄对,这样其他人才能真正理解你在说什么。