void getInputData(void) {
char c = 0;
while (c != 'x') {
printf("Enter a letter:");
fflush(stdout);
c = getchar();
printf("You entered: %c \n",c);
fflush(stdout);
getchar();
}
printf("\nYou entered x and the loop exited\n");
}
为什么这个函数最后的 getchar()
是必须的?我不明白为什么它在那里。
最佳答案
假设您在您的程序中使用行缓冲,完美用户输入每次跟随字母直到命中“x”:
\n
H\n
我\n
S\n
x\n
只有当您从键盘上按 ENTER/RETURN 键时,发送数据才有效。您应该处理 \n
换行符以获得下一个字符,这就是为什么需要使用 getchar()
的原因。
关于c - 为什么这个函数最后需要 getchar()?我不明白为什么它在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40349902/