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 prints each pair with %d and a tab character. The output is correct but bare: there are no column labels, so a reader staring at two columns of numbers has no idea which is which.

Exercise 1-3 asks you to fix exactly that by adding a heading row above the table. The solution is a single printf call placed before the loop. While you are at it, replacing the raw %d format with width specifiers (%3d for Fahrenheit, %6d for Celsius) makes the columns align neatly — the same improvement K&R introduce a few paragraphs later in the book.

Solution

/* Compile: gcc -ansi -Wall ex1-3.c -o ex1-3 */
#include <stdio.h>

/* print Fahrenheit-Celsius table with a heading row */
int main(void)
{
    int fahr, celsius;
    int lower, upper, step;

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

    printf("Fahr  Celsius\n");   /* heading above the table */

    fahr = lower;
    while (fahr <= upper) {
        celsius = 5 * (fahr - 32) / 9;
        printf("%3d  %6d\n", fahr, celsius);
        fahr = fahr + step;
    }
    return 0;
}

Two things changed from the base program:

  1. The headingprintf("Fahr Celsius\n"); before the loop prints the column labels once.
  2. Width specifiers%3d reserves a three-character field for Fahrenheit (the widest value is 300, which is three digits), and %6d reserves six characters for Celsius. Both right-justify their values, so the digits line up in clean columns.

Compile and Run

gcc -ansi -Wall ex1-3.c -o ex1-3
./ex1-3

Sample Output

Fahr  Celsius
  0      -17
 20       -6
 40        4
 60       15
 80       26
100       37
120       48
140       60
160       71
180       82
200       93
220      104
240      115
260      126
280      137
300      148

The heading row sits flush above the data. Each Fahrenheit value is right-justified in a three-character field and each Celsius value in a six-character field, so the columns align regardless of how many digits each number uses.

What This Exercise Teaches

  • printf width specifiers%3d and %6d right-justify integers into fixed-width fields. Any value narrower than the field is padded with spaces on the left; any value wider overflows (so choose your widths to fit the largest expected value).
  • Table-style output — a single header printf before a loop is the idiomatic C pattern for column labels. The structure is always: initialise → print heading → loop over data.
  • Integer division truncates toward zero5 * (fahr - 32) / 9 gives approximate integer Celsius values. Multiplying by 5 first avoids losing the fractional part too early (integer division is applied last).
  • Program structure — setting lower, upper, and step as named variables rather than magic numbers makes the program easier to read and modify, a habit K&R reinforce throughout Chapter 1.

Set Up Your C Environment

To compile and run this solution you need GCC. If you have not set up C on your machine yet, these guides walk you through the whole process:

← Exercise 1-2  | 
Chapter 1 Solutions  | 
Exercise 1-4 →

Book:
The C Programming Language, 2nd Ed — Kernighan & Ritchie

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>