K&R C Exercise 1-15: Rewrite Temperature Conversion Using a Function

Exercise 1-15. Rewrite the temperature conversion program of Section 1.2 to use a function.

Section 1.2 of K&R embeds the conversion formula directly inside main. It works — but it ties the formula to one spot in the code. This exercise asks you to extract that logic into a separate function called celsius. The payoff is real: once the conversion lives in its own function, you can call it from anywhere in the program without copying the formula. More importantly, this is your introduction to the three-part function pattern that C uses throughout: the declaration (prototype), the call, and the definition. Understanding why all three exist — and in what order — is the core lesson of this exercise.

The Solution

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

float celsius(int fahr);   /* function prototype: tells the compiler
                              what celsius() accepts and returns */

int main(void)
{
    int fahr;

    printf("Fahr\tCelsius\n");
    for (fahr = 0; fahr <= 300; fahr += 20)
        printf("%3d\t%6.1f\n", fahr, celsius(fahr));
    return 0;
}

/* celsius: convert Fahrenheit to Celsius */
float celsius(int fahr)
{
    return (5.0 / 9.0) * (fahr - 32.0);
}

How the Function Pattern Works

C processes source files from top to bottom. When the compiler reaches the celsius(fahr) call inside main, it has not yet seen the full function definition (which comes after). Without guidance, it would have to guess the return type and argument types — and it would guess wrong. The prototype on line 3 solves this:

float celsius(int fahr);

This one line tells the compiler everything it needs to know: celsius takes one int argument and returns a float. Now the call in main can be type-checked correctly. The actual definition — where the body is written — comes after main. This split (prototype before, definition after) is the layout K&R uses throughout the book, and it is standard practice in real C programs.

Why int fahr, not float?

The original Section 1.2 program used float fahr. Here we use int fahr, which matches how K&R phrases the exercise. The loop steps are all whole numbers (0, 20, 40, …, 300), so an int is the right type. Inside celsius, the subtraction fahr - 32.0 promotes the integer to a double automatically before the arithmetic, so the result is precise. The function then returns a float.

The for Loop

The original while loop from Section 1.2 required three separate statements: initialise fahr, check the condition, and update fahr at the end of the body. The for loop gathers all three into one line:

for (fahr = 0; fahr <= 300; fahr += 20)

The three fields — init, condition, increment — map directly onto what the while loop did across several lines. There is no functional difference; the for is simply more compact and keeps all the loop-control logic in one place.

Compile and Run

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

Sample Output

Fahr   Celsius
  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

What This Exercise Teaches

  • Function prototypes: In ANSI C, a function must be declared before it is called. The prototype float celsius(int fahr); gives the compiler the type information it needs without yet providing the body.
  • Separation of declaration and definition: The prototype and the full definition are two different things. K&R consistently places definitions after main; the prototype bridges the gap.
  • Returning a value: return (5.0 / 9.0) * (fahr - 32.0); — the function computes a value and hands it back to the caller. The caller (printf) receives it as a float.
  • Reusability: The conversion formula now exists in exactly one place. If you later discover an error in it, you fix it once. This is why functions exist.
  • The for statement: Init, condition, and increment collected into one construct — a compact alternative to the while loop used in Section 1.2.

Set Up Your C Environment

To compile and run this solution, you need GCC installed. If you haven’t set up C on your machine yet:

← Exercise 1-14  | 
Chapter 1 Solutions  | 
Exercise 1-16 →

Book:

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

2 comments on “K&R C Exercise 1-15: Rewrite Temperature Conversion Using a Function

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>