为什么fflush(标准输入)功能无法正常工作? [英] How come fflush(stdin) function is not working?
本文介绍了为什么fflush(标准输入)功能无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的主要问题是,为什么是它的fflush(标准输入);功能不工作?
每当我运行了code,我没能获得与空间前的第二个输入。的Hello World而是我得到你好?谢谢
的#include<&stdio.h中GT;主要(){ INT X;
双Y;
字符字符串[100]; / * *字符串输入 * / 的printf(请输入一个字:);
scanf函数(%S,字符串); //注意没有和放大器;字符串之前* /
的printf(您输入的词是>>%S<< \\ n); 的printf(请输入一段话:);
fflush(标准输入); //< ----由于某种原因,此功能无法正常工作
scanf函数(%[^ \\ n],字符串); //读到一个换行符(多字) 的printf(您输入的文本是>>%S<< \\ n); 的getchar();
}
解决方案
由于 fflush(标准输入)
是未定义的行为。 fflush()
仅由输出流的C标准定义,并更新流,其中最后一次操作是输出。
My main question is why is it that the fflush(stdin); function not working? Whenever I run the the code, I am not able to get the second input with space ex. Hello World but instead I get Hello?? thanks
#include <stdio.h>
main(){
int x;
double y;
char string[100];
/*
* string input
*/
printf("Enter one word: ");
scanf("%s", string); // note there is no & before string */
printf("The word you entered was >>%s<<\n");
printf("Enter many words: ");
fflush(stdin); // <---- for some reason this function is not working
scanf("%[^\n]", string); // read up to a newline (multiple words)
printf("The text you entered was >>%s<<\n");
getchar();
}
解决方案
Because fflush(stdin)
is undefined behavior. fflush()
is only defined by the C standard for output streams, and update streams where the last operation was an output.
这篇关于为什么fflush(标准输入)功能无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文