我正在研究如何scanf工作。
扫描其他类型变量后,char 变量通过getchar()or存储一个空格('\n')scanf("%c")。为了防止这种情况,他们应该清除缓冲区。我做到了rewind(stdin)
尽管 stdin 被倒带,但先前的输入值仍保留在缓冲区中。我可以正常使用以前的值做一些事情。(没有运行时错误)但是如果我再试scanf一次,即使缓冲区中有一个正常值,scanf 也会扫描一个新值。scanf 如何确定是否应该扫描新值?
我用下面的代码找到了这个机制。
#include <stdio.h>
#define p stdin
int main() {
int x;
char ch;
void* A, * B, * C, * D, * E;
A = p->_Placeholder;
printf("A : %p\n", A);//first time, it shows 0000
scanf_s("%d", &x);
B = p->_Placeholder;
printf("B : %p\n", B);//after scanned something, I think it's begin point of buffer which is assigned for this process
rewind(stdin);//rewind _Placeholder
C = p->_Placeholder; …Run Code Online (Sandbox Code Playgroud)