scanf 读取后输入缓冲区是否被清除?
Does the input buffer gets cleared after scanf reads?
输入缓冲区是否在 scanf
读取后被清除?
#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?
没有,3被消耗了
您不能重新读取它(作为 int 或其他方式)。
如果您输入 "3<enter>"
,则 3 被消耗,缓冲区仅包含 "<enter>"
。然后您键入 "4<enter>"
,它被添加到缓冲区。第 2 次 scanf(*) 消耗初始输入和 4 离开 "<enter>"
用于下一次输入操作。
(*) 转换说明符 "%d"
跳过可选的前导空格并(尝试)将输入的其余部分转换为整数(如果没有发生错误)。
So when I gave the input 4 was 3 present in the input buffer?
不,不是。
scanf()
从标准输入读取(并使用)直到找到匹配指定格式(在您的情况下为整数)。该格式也会被转换和使用。
输入缓冲区是否在 scanf
读取后被清除?
#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?
没有,3被消耗了
您不能重新读取它(作为 int 或其他方式)。
如果您输入 "3<enter>"
,则 3 被消耗,缓冲区仅包含 "<enter>"
。然后您键入 "4<enter>"
,它被添加到缓冲区。第 2 次 scanf(*) 消耗初始输入和 4 离开 "<enter>"
用于下一次输入操作。
(*) 转换说明符 "%d"
跳过可选的前导空格并(尝试)将输入的其余部分转换为整数(如果没有发生错误)。
So when I gave the input 4 was 3 present in the input buffer?
不,不是。
scanf()
从标准输入读取(并使用)直到找到匹配指定格式(在您的情况下为整数)。该格式也会被转换和使用。