Exercise 4.9 - getch and ungetch handling EOF Character

Question

Our getch and ungetch do not handle a pushed-back EOF correctly. Decide what their properties ought to be if an EOF is pushed back, then implement your design.

/**
 * Exercise 4.9 of The C Programming Language by Brian Kernighan and Dennis
 * Ritchie
 *
 * Our getch and ungetch do not handle a pushed-back EOF correctly.
 *
 * Decide what their properties ought to be if an EOF is pushed back, then
 * implement your design.
 *
 */

#include <stdio.h>
#include <stdlib.h>

#define BUFSIZE 100

char buf[BUFSIZE];

int bufp = 0;

int getch(void) { return (bufp > 0) ? buf[--bufp] : getchar(); }

void ungetch(int c) {
    if (bufp >= BUFSIZE) {
        printf("ungetch: too many characters\n");
    } else {
        buf[bufp++] = c;
    }
}

int main(int argc, char *argv[]) {
    int c;
    c = '*';

    while ((c = getch()) != EOF) {
        putchar(c);
    }

    return 0;
}

Explanation

The previous getch and ungetch functions declared buf as char buf[BUFSIZ]. This has a limitation wherein the when an EOF character is encountered, it wont be stored in the buffer. The EOF character is an integer type. This problem can be solved by declaring our buf to be of integer type, like int buf[BUFSIZE] and ungetch(c) will store the character c, including EOF, now in an integer array.

Visualize It

Try It