Exercise 1-4. Write a program to print the corresponding Celsius to Fahrenheit table.
Approach
K&R Chapter 1 opens with a Fahrenheit-to-Celsius table, converting F values to Celsius using C = 5 × (F − 32) / 9. This exercise asks for the inverse: step through Celsius values (0, 20, 40, …, 300) and compute the corresponding Fahrenheit temperature. Rearranging the original formula gives the conversion directly:
C = 5 × (F − 32) / 9 ⇒ 9C = 5 × (F − 32) ⇒ 9C / 5 = F − 32 ⇒ F = 9C / 5 + 32
Declaring both variables as float keeps the arithmetic in floating-point throughout. An integer version would round intermediate results — harmless here because all Celsius steps are multiples of 5, but the wrong habit for more general formulas. For example, if you stepped by 3 and tried 9 * celsius / 5 with integers, the division would silently truncate the remainder. The %7.1f and %6.1f format specifiers right-align both columns with one decimal place, producing a clean table no matter how many digits the values have.
Solution
/* Compile: gcc -ansi -Wall ex1-4.c -o ex1-4 */
#include <stdio.h>
int main(void)
{
float celsius, fahr;
int lower, upper, step;
lower = 0;
upper = 300;
step = 20;
printf("Celsius\tFahr\n");
celsius = lower;
while (celsius <= upper) {
fahr = (9.0 * celsius / 5.0) + 32.0;
printf("%7.1f\t%6.1f\n", celsius, fahr);
celsius = celsius + step;
}
return 0;
}
The formula (9.0 * celsius / 5.0) + 32.0 uses the literal 9.0 to force floating-point multiplication from the start. Writing 9 * celsius / 5 instead would still be fine here because celsius is already float, but using floating-point constants is an explicit signal of intent and avoids subtle bugs if the variable type ever changes.
Compile and Run
gcc -ansi -Wall ex1-4.c -o ex1-4
./ex1-4
Sample Output
Celsius Fahr
0.0 32.0
20.0 68.0
40.0 104.0
60.0 140.0
80.0 176.0
100.0 212.0
120.0 248.0
140.0 284.0
160.0 320.0
180.0 356.0
200.0 392.0
220.0 428.0
240.0 464.0
260.0 500.0
280.0 536.0
300.0 572.0
Two landmarks worth noting: 0 °C = 32 °F (water’s freezing point) and 100 °C = 212 °F (boiling point at sea level). These are quick sanity checks you can use to verify the formula is correct before trusting the rest of the table.
What This Exercise Teaches
- Deriving the inverse formula: rearranging
C = 5(F−32)/9algebraically to getF = 9C/5 + 32— a core skill whenever you need to reverse a computation. - Floating-point variables: declaring
celsiusandfahrasfloatinstead ofintavoids integer truncation and produces accurate decimal output for non-divisible values. - Formatted table output:
printf("%7.1f\t%6.1f\n", celsius, fahr)right-aligns each column with one decimal place, demonstrating how field-width and precision specifiers work together. - Loop with a floating-point control variable: the
whileloop incrementscelsiusbystepeach iteration — the same table-generation pattern K&R use throughout Chapter 1 and the foundation for every numeric range loop you will write in C.
Set Up Your C Environment
To compile and run this program you need GCC installed. If you are new to C development, these guides will get you started:
- Complete C Development Environment Setup — recommended starting point
- Install GCC on Windows 11
- Install GCC on macOS
- Install GCC on Ubuntu/Linux
- VS Code for C Programming — recommended editor
← Exercise 1-3 |
Chapter 1 Solutions |
Exercise 1-5 →
Book:
The C Programming Language, 2nd Ed — Kernighan & Ritchie
2 comments on “K&R C Exercise 1-4: Celsius to Fahrenheit Table”
I have a wordpress blog and I would really like to remove almost everything (it’s managed on my own domain and hosting) because I would really like to delete my site. How can I conserve my blog posts in a file or some thing in my files (offline) because I don’t want to delete everything completely. Can I do that at once, if I can even do it? Many thanks!.
Hi Lauren,
I understand your question. You can take a back up of your entire site. Not just the posts, pages and comments. Download all of your site including theme customizations etc.
Email me your details or contact on our Facebook page. I can help you with this.