我正在尝试从控制台读取一个字符(在 while 循环内)。但它读取了不止一次。
输入:
a
输出:
char : a char : char : '
代码:
while(..)
{
char in;
scanf("%c",&in);
}
我怎样才能只读取'a'?
最佳答案
scanf("%c",&in);
留下一个换行符,在下一次迭代中使用。
将其更改为:
scanf(" %c",&in); // Notice the whitespace in the format string
告诉 scanf 忽略空格。
或
scanf(" %c",&in);
getchar(); // To consume the newline
关于c - 在C中读取单个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14419954/