fflush(stdin)

来源:互联网 发布:人工智能成功案例 编辑:程序博客网 时间:2024/06/05 21:55
#include<stdio.h>int main(){    char arry[20], ch;    printf("Plase input a string:");    scanf("%s", arry);    printf("Plase input a chareacter:");    scanf("%c", &ch);    .......}

这段看起来好像没有问题,编译也能通过,但是运行时不能输入字符,这是因为scanf是从缓冲区里面读取字符,当输入了一个字符串后没有清空缓冲区,就会直接从缓冲区里面读取,所以需要在第一个scanf后面加上fflush(stdin);

0 0
原创粉丝点击