Hi, i have problem with these functions: getch() and ungetch(), pag 67 english 2 version K&R.
I do not post the entire code of the polish notation calculator, i assume you already know.Code:#define BUFSIZE 100 char buf[BUFSIZE]; /* buffer for ungetch */ int bufp = 0; /* next free position in buf */ int getch(void) /* get a (possibly pushed-back) character */ { return (bufp > 0) ? buf[--bufp] : getchar(); } void ungetch(int c) /* push character back on input */ { if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++] = c; }
What i have understood is:
when getch is called, if the chars counter bufp is > 0, then return the prevoius char buf[--bufp], else call getchar(), this id done to not get back the wrong char, for example a non-digit;
when ungetch is called it stores the characters in the array buf[buf++].
I would be grateful if someone can explain how these functions work, with an example.
Thanks.