Code of the Day
BeginnerHello, C

Lab: Temperature converter

Build a command-line temperature converter that converts between Celsius, Fahrenheit, and Kelvin using what you have learned so far.

Lab · optionalCBeginner20 min

This lab puts together everything from the Hello, C module: variables, primitive types, arithmetic expressions, and printf. You will build a temperature converter that works with three scales and prints a formatted table of results.

Goal

Write a program convert.c that:

  1. Accepts a Celsius temperature as a hardcoded value (e.g. double celsius = 100.0;)
  2. Converts it to Fahrenheit and Kelvin using the correct formulas
  3. Prints a formatted table showing all three values

Formulas:

  • Fahrenheit = (Celsius × 9 / 5) + 32
  • Kelvin = Celsius + 273.15

Expected output

For an input of 100.0°C, your program should print:

Temperature Conversions
-----------------------
Celsius:    100.00 C
Fahrenheit: 212.00 F
Kelvin:     373.15 K

Spacing and decimal places should match exactly.

Starter notes

Start from this skeleton:

#include <stdio.h>

int main(void) {
    double celsius = 100.0;

    /* Compute fahrenheit and kelvin here */

    /* Print the table */
    printf("Temperature Conversions\n");
    printf("-----------------------\n");
    /* Add printf calls for each temperature */

    return 0;
}

Things to get right:

  • Use double for all three values to avoid integer truncation.
  • Use %.2f to print exactly two decimal places.
  • The label Fahrenheit: is 11 characters; use %-11s or pad manually to align the columns.

Extension challenges

Once the basic version works, try:

  1. Print a conversion table. Instead of a single value, print a table from −40°C to 100°C in 10-degree steps.
  2. Reverse conversions. Add Fahrenheit-to-Celsius and Kelvin-to-Celsius functions (you will need to read ahead to the Functions module, or use a separate variable).
  3. Column headers. Add a header row C F K above the table.

A worked solution

When you have tried on your own, compare with this approach:

#include <stdio.h>

int main(void) {
    double celsius = 100.0;
    double fahrenheit = (celsius * 9.0 / 5.0) + 32.0;
    double kelvin = celsius + 273.15;

    printf("Temperature Conversions\n");
    printf("-----------------------\n");
    printf("Celsius:    %.2f C\n", celsius);
    printf("Fahrenheit: %.2f F\n", fahrenheit);
    printf("Kelvin:     %.2f K\n", kelvin);

    return 0;
}

Compile and run:

gcc -Wall -Wextra convert.c -o convert
./convert

Try the table extension. It requires a loop, which you have not learned yet — but you can fake it with repeated assignments and printf calls. After working through the Control Flow module, come back and refactor it to use a for loop.

What you practised

  • Declaring and initialising double variables
  • Using arithmetic expressions with the correct operators
  • Controlling printf output format with %.2f
  • Organising a program with clear sections and comments

The next module is Control flowif/else, switch, loops, and break/continue. These let your programs make decisions and repeat work, which is when they become genuinely useful.

Finished reading? Mark it complete to track your progress.

On this page