5.1.1.c 488 B

1234567891011121314151617181920
  1. #include <stdio.h>
  2. #define BUFSIZE 100
  3. #define SIZE 16
  4. char buf[BUFSIZE]; /* buffer for ungetch */
  5. int bufp1 = 0; /* next free position in buf */
  6. int getch(void) /* get a (possibly pushed-back) character */
  7. {
  8. return (bufp1 > 0) ? buf[--bufp1] : getchar();
  9. }
  10. void ungetch(int c) /* push character back on input */
  11. {
  12. if (bufp1 >= BUFSIZE)
  13. printf("ungetch: too many characters\n");
  14. else
  15. buf[bufp1++] = c;
  16. }