The Second K&R Program (That No One Talks About)

The original K&R, the first C programming book — and truly the programming book all others are based upon, is famous for its “Hello, world!” program. It’s the first program in the book. But what about the second program? Do you know what it is?

First, the book’s proper title is The C Programming Language, Copyright 1978. I have the first edition, 20th printing, which I bought in person at San Diego Technical Books. Sadly, this once awesome bookstore is gone now, but I still have plenty of books I purchased back in its heyday.

Second, the code presented as the book’s second program doesn’t compile. Here’s the code:

2024_10_19-Lesson-a.c

/* print Fahrenheit-Celsius table
   for f = 0, 20, ..., 300 */
main()
{
    int lower, upper, step;
    float fahr, celsius;

    lower = 0;        /* lower limit of temperature table */
    upper = 300;     /* upper limit */
    step = 20;        /* step size */

    fahr = lower;
    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%4.0f %6.1f\n",fahr,celsius);
        fahr = fahr + step;
    }
}

A few things you may notice right away, since it’s been almost 50 years since the book was published: The main() function isn’t declared as an int. Further, no return statement is present. This approach is how C code was written for some time. Back when I first learned C, main() wasn’t typed, or often typed void, and the return statement was optional.

Another thing to notice, and the reason why the code won’t compile, is that the printf() statement requires the stdio.h header file, which is missing. The reason stated in the book is that the #include directive isn’t covered until Chapter 7. Still this is the second program presented in the book.

The following code is my attempt to update the original program to the ANSI standard.

2024_10_19-Lesson-b.c

/* print Fahrenheit-Celsius table
   for f = 0, 20, ..., 300 */
#include <stdio.h>

int main()
{
    int lower, upper, step;
    float fahr, celsius;

    lower = 0;        /* lower limit of temperature table */
    upper = 300;     /* upper limit */
    step = 20;        /* step size */

    fahr = lower;
    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%4.0f %6.1f\n",fahr,celsius);
        fahr = fahr + step;
    }
    return 0;
}

I’ve added the #include directive and a return statement. Otherwise, this is the same code published 47 years ago. And it runs when compiled on a modern C compiler. Here’s the output:

   0  -17.8
  20   -6.7
  40    4.4
  60   15.6
  80   26.7
 100   37.8
 120   48.9
 140   60.0
 160   71.1
 180   82.2
 200   93.3
 220  104.4
 240  115.6
 260  126.7
 280  137.8
 300  148.9

If you truly want to go back in time, you can compile the code with the -ansi switch. If so, you can remove the int before main() and pull out the return statement as well. (Originally, I commented out these items using the // style comments — but these aren’t allowed in ANSI C!)

As I wrote earlier, K&R is one of the original computer books. To prove how other programming books are based on it is demonstrated by this very program. When I learned BASIC on the TRS-80, I used the book shown in Figure 1, which came with my Model III microcomputer. On Page 25 of this book is the BASIC Celsius-to-Fahrenheit conversion program, shown in Figure 2.

Figure 1. The BASIC programming instruction manual that came with my TRS-80 Model III computer. (Yes, I still own this copy.)

Figure 2. Page 25 of the BASIC programming manual, which lists the Celsius-to-Fahrenheit conversion program at the bottom of the page.

As far as I can tell, I’ve never used this type of temperature conversion program in any of my programming books. It’s tempting, but I don’t think I’ve ever done it.

Leave a Reply