scanf读取后输入缓冲区是否被清除? [英] Does the input buffer gets cleared after scanf reads?

查看:264
本文介绍了scanf读取后输入缓冲区是否被清除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

读取 scanf 后是否清除输入缓冲区?

Does the input buffer gets cleared after scanf reads?

#include <stdio.h>

int main(void) 
{
    int a, b;
    scanf("%d", &a);//I inputted 3
    scanf("%d", &b);//I inputted 4
}

所以当我输入 4 时, 3 是否存在于输入缓冲区中?

So when I gave the input 4 was 3 present in the input buffer?

推荐答案


当我给输入4时,输入缓冲区中是否有3个?

So when I gave the input 4 was 3 present in the input buffer?

否,这3被消耗了。

您不能重新读取它(以int或其他方式读取)。

You cannot re-read it (as int or otherwise).

如果输入 3< enter> ; 被消耗,并且缓冲区仅包含< enter> 。然后,您输入 4< enter> 添加到缓冲区中。第2个scanf(*)消耗了初始输入,而第4个剩余的< enter> 用于下一个输入操作。

If you input "3<enter>" the 3 is consumed and the buffer contains just the "<enter>". You then type "4<enter>" which is added to the buffer. The 2nd scanf (*) consumes the initial enter and the 4 leaving "<enter>" for the next input operation.

(*)转换说明符%d 跳过可选的前导空格,并(尝试)将其余输入转换为整数(如果没有发生错误)

(*) the conversion specifier "%d" skips optional leading whitespace and (tries to) converts the rest of the input to integer (if no errors occur).

这篇关于scanf读取后输入缓冲区是否被清除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆