K&R C Programs Exercise 4-9

Exercise 4-9. Our getch and ungetch do not handle a pushed-back EOF correctly. Decide what their properties should be if an EOF is pushed back, then implement your decision. The bug: K&R’s original implementation uses char buf[BUFSIZE]. When ungetch(EOF) is called, EOF (typically -1) is stored as char. On platforms where char is unsigned, -1 …

K&R C Exercise 1-7: Print the Value of EOF

Exercise 1-7. Write a program to print the value of EOF. This is one of those exercises that looks trivial — one printf and you’re done — but it opens the door to one of the most important design decisions in the C standard library. Before you can really understand why Chapter 1’s input loops …

K&R C Exercise 1-6: Verify getchar() != EOF is 0 or 1

Exercise 1-6. Verify that the expression getchar() != EOF is 0 or 1. What This Exercise Is Really About This is not a “write a loop” exercise — it is a verification exercise, and the thing being verified is a fundamental rule of C that surprises many beginners: every relational expression in C evaluates to …