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 …

K\&R C Exercise 1-5: Reverse Temperature Table (300 to 0)

Exercise 1-5. Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0. Approach The original K&R temperature program counts up: it starts fahr at 0, adds the step each iteration, and stops when fahr > 300. Reversing the table means changing exactly three things: (1) start …

K&R C Exercise 1-3: Add a Heading to the Temperature Table

Exercise 1-3. Modify the temperature conversion program to print a heading above the table. What the Base Program Does K&R Section 1.2 introduces a program that prints a Fahrenheit-to-Celsius conversion table from 0 to 300 in steps of 20. It uses integer arithmetic — celsius = 5 * (fahr – 32) / 9 — and …

K&R C Exercise 1-1c: Reading gcc Error Messages

Exercise 1-1. Run the “hello, world” program on your system. Experiment with leaving out parts of the program to see what error messages you get. This page covers the error-experimentation part of Exercise 1-1. The baseline working program is in Exercise 1-1a. Here we deliberately break it five different ways — and more importantly, learn …

K&R C Exercise 1-1b: Experimenting with printf

Exercise 1-1. Run the “hello, world” program on your system. Experiment with leaving out parts of the program to see what error messages you get. This page covers the printf side of Exercise 1-1: what the format string actually does, how escape sequences work, and what happens when you modify or multiply printf calls. For …