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 / 5 with 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 float and then printing many decimals. double is the default for this kind of arithmetic in C.
  • Linking extra libraries. These formulas need only #include <stdio.h>. You do not needmath.h for multiply and add.

Practice

  1. Add celsius_to_kelvin output for -40, 0, and 100 next to the Fahrenheit values you already print.
  2. Write kelvin_to_celsius and check that kelvin_to_celsius(celsius_to_kelvin(21.0)) is 21.
  3. Reject Celsius below -273.15 by printing an error from main instead of converting.

FAQ: C Project: Temperature Converter

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c temperature project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c temperature project in this C C lesson (C Project: Temperature Converter).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.