K&R C Programs Exercise 4-11

Exercise 4-11. Modify getop so that it doesn’t need to use ungetch. Hint: use an internal static variable. In the original calculator, getop calls ungetch once: when it reads a non-digit character to terminate a number, it pushes that character back so the next call to getop will see it. The fix: instead of pushing …

K&R C Programs Exercise 4-8

Exercise 4-8. Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly. The original K&R implementation uses an array buf[BUFSIZE] and an index bufp to support multiple pushed-back characters. If only one character of pushback is ever needed, the array becomes unnecessary. A single static int buf suffices …

K&R C Programs Exercise 4-7

Exercise 4-7. Write a routine ungets(s) that will push back an entire string onto the input. Should ungets know about buf and bufp, or should it just use ungetch? The answer is: ungets should use ungetch, not access buf and bufp directly. This is the principle of information hiding — getch and ungetch own the …