c语言,这个程序里,ch=getchar()后面的getchar()起什么作用

#include <stdio.h>

int is_within(char *p, char ch);

int main(void)
{
char str[81];
char ch;
do
{
puts("input range string:");
gets(str);
puts("input match char:");
ch = getchar();
getchar();
if (is_within(str, ch)) puts("Find!");
else puts("Can't find!");
puts("input any char except q to go on.");
ch = getchar();
getchar();
} while (ch != 'q');

puts("Quit.");
return 0;
}

如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取.也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键.追问

输入一个字符后敲回车,换行符没有被丢弃吗

追答

不好意思刚刚那个错了,getchar()会读取到回车键

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网