能有人解释一下ungetch的作用吗?这段代码来自K&R第4章,用于创建一个逆波兰计算器。
我在不调用ungetch的情况下运行了程序,在我的测试中它仍然可以正常工作。
int getch(void) /* get a (possibly pushed back) character */
{
if (bufp > 0)
{
return buf[--bufp];
}
else
{
return getchar();
}
}
void ungetch(int c) /* push character back on input */
{
if (bufp >= BUFSIZE)
{
printf("ungetch: too many characters\n");
}
else
{
buf[bufp++] = c;
}
}
(我已经移除了getch中的三目运算符,以使代码更清晰。)
bufp []
每次只读取一个字符,并且给定BUFSIZE = 100
,那么在这个K&R
的特定示例中,什么时候执行printf(“ungetch:too many characters \ n”);
? - noufal