C Tutorial
C Project: Temperature Converter
Convert Celsius to Fahrenheit and Kelvin with functions so the formulas stay in one place.
What you will build
Temperature conversion is a pair of formulas. Celsius to Fahrenheit is multiply by 9, divide by 5, then add 32. Celsius to Kelvin is add 273.15. Put each formula in its own function so main never repeats the arithmetic. If a constant is wrong, you fix it once.
The examples feed hardcoded Celsius values into the functions and print a table. Compile them withTry it in C at /c/try. That is gcc for C.
One formula, one function
A function has a return type, a name, and a parameter. Here the parameter is Celsius as adouble. The body is a single return. Call the function from printf or store the result.
Example
#include <stdio.h>
double celsius_to_fahrenheit(double c) {
return c * 9.0 / 5.0 + 32.0;
}
int main(void) {
double c = 100.0;
printf("%.1f C = %.1f F\n", c, celsius_to_fahrenheit(c));
printf("%.1f C = %.1f F\n", 0.0, celsius_to_fahrenheit(0.0));
return 0;
}Boiling water is 100 C and 212 F. Freezing is 0 C and 32 F. If those two lines are wrong, the 9/5 factor or the 32 is wrong. Write 9.0 / 5.0, not 9 / 5. Integer division would become 1.
Celsius to Kelvin
Kelvin is an offset, not a scale change. Absolute zero is 0 K, which is -273.15 C. Adding 273.15 to Celsius gives Kelvin. Keep that number in one function so you do not type 273.15 in five different print statements.
Example
#include <stdio.h>
double celsius_to_kelvin(double c) {
return c + 273.15;
}
int main(void) {
printf("%.2f K\n", celsius_to_kelvin(0.0));
printf("%.2f K\n", celsius_to_kelvin(25.0));
printf("%.2f K\n", celsius_to_kelvin(-273.15));
return 0;
}The third line should print 0.00 K. Compile at /c/try and check it. Stay in the C editor; do not paste this into /try, /html/try, or /cpp/try.
A conversion table
Store several Celsius readings in an array. For each reading, call both functions. Print three columns: C, F, and K. This is the program you want as the project result — small functions, one loop, no duplicated formulas.
Example
#include <stdio.h>
double celsius_to_fahrenheit(double c) {
return c * 9.0 / 5.0 + 32.0;
}
double celsius_to_kelvin(double c) {
return c + 273.15;
}
int main(void) {
double celsius[] = {-40.0, 0.0, 21.0, 37.0, 100.0};
int n = (int)(sizeof celsius / sizeof celsius[0]);
printf("%8s %8s %8s\n", "C", "F", "K");
for (int i = 0; i < n; i++) {
double c = celsius[i];
printf("%8.2f %8.2f %8.2f\n", c, celsius_to_fahrenheit(c),
celsius_to_kelvin(c));
}
return 0;
}-40 C equals -40 F; that reading is a useful check. 37 C is typical body temperature. 21 C is a mild room. Read the F and K columns and confirm they match the two functions, not a third copy of the math inmain.
Keep the formulas off the printf line
You could write c * 9.0 / 5.0 + 32.0 inside printf. Do not. The table above already proves the point: two call sites, one definition. If you later add Fahrenheit-to-Celsius, add another function rather than inverting the expression inline in the loop.
Example
#include <stdio.h>
double celsius_to_fahrenheit(double c) {
return c * 9.0 / 5.0 + 32.0;
}
double fahrenheit_to_celsius(double f) {
return (f - 32.0) * 5.0 / 9.0;
}
int main(void) {
double room_f = 70.0;
double room_c = fahrenheit_to_celsius(room_f);
printf("%.1f F -> %.2f C\n", room_f, room_c);
printf("back to F: %.1f\n", celsius_to_fahrenheit(room_c));
return 0;
}Round-trip conversion will not always land on the same printed digits because of binary floating point, but 70 F should come back very close to 70 F. That is a good sanity check when you add a new helper.
Common mistakes
9 / 5with integers. The quotient is 1, so every Fahrenheit value is Celsius plus 32.- Mixing the offset: adding 32 before multiplying, or adding 273.15 to Fahrenheit. Each function should do one scale only.
- Using
floatand then printing many decimals.doubleis the default for this kind of arithmetic in C. - Linking extra libraries. These formulas need only
#include <stdio.h>. You do not needmath.hfor multiply and add.
Practice
- Add
celsius_to_kelvinoutput for -40, 0, and 100 next to the Fahrenheit values you already print. - Write
kelvin_to_celsiusand check thatkelvin_to_celsius(celsius_to_kelvin(21.0))is 21. - Reject Celsius below -273.15 by printing an error from
maininstead of converting.