======================================================= 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. .. literalinclude:: cprogs/ex_4.9_getch_ungetch_eof.c :language: c Explanation =========== The previous `getch` and `ungetch` functions declared buf as `char buf[BUFSIZE]`. 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 ============ .. raw:: html Try It ====== .. raw:: html