码迷,mamicode.com
首页 > 系统相关 > 详细

解决linux下fflush(stdin)无效

时间:2020-06-05 10:27:22      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:tchar   fflush   clean   缓冲区   linux   bsp   整数   color   解决   

scanf("%d", &i); 只能接受整数,如果用户输入了字母,则这个字母会遗留在“输入缓冲区”中。
因为缓冲中有数据,故而 scanf 函数不会等待用户输入,直接就去缓冲中读取,可是缓冲中的却是字母,
这个字母再次被遗留在缓冲中,如此反复,从而导致不停地输出

 

void clean_stdin(void)
{
    int c;
    do {
        c = getchar();
    } while (c != \n && c != EOF);
}

 

解决linux下fflush(stdin)无效

标签:tchar   fflush   clean   缓冲区   linux   bsp   整数   color   解决   

原文地址:https://www.cnblogs.com/sdushmily/p/13048127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!