【发布时间】:2013-07-01 12:48:25
【问题描述】:
首先我调用 getchar() 并在标准输入/输出上输入一些字符,因为 scanf() 也可以获取这些字符,我想在调用 scanf() 之前清空缓冲区,这是程序。
int main()
{
getchar(); // i input some characters here , "abcdefgh"
------ // here i need some statement that will empty standard input/output.
int a;
scanf("%d",&a); // so if buffer is empty, this prompt me to enter chacter.
// if i enter 7
printf("%d",a); // this should print 7
}
【问题讨论】:
-
您可以在
stdin上使用fflush,但您应该知道它是非标准的,并且可能不适用于所有系统。 -
printf("%d\n", a); //这应该打印 7...
-
请详细说明您的目标。在我看来
getchar()是通过等待输入来执行的。您开始输入“abc ...”。但是在你输入 'a' 之后,getchar()完成并且你想要的“空标准输入/输出”将在你有时间输入“bc ...”之前执行。因此在scanf()执行之前不会刷新“bc ...”。你的想法?