Skip to content
C project #03Beginner

Unit Converter

Convert temperature, length and weight units, and print formatted conversion tables — driven by a table of function pointers.

Estimated time
~2 hours to build
Files
5 files · 308 lines
Technologies
C11, <stdio.h>

Introduction

A unit converter looks simple, but the way it is organised matters. This version keeps every conversion in one table that pairs a label with a function, so adding a new unit is a one-line change and the menu builds itself.

Problem statement

Create a converter that offers at least eight conversions (temperature, distance, weight, length), converts a single value or prints a table for a range of values, and warns about physically impossible results such as temperatures below absolute zero.

Objectives

  • Store related data and behaviour in an array of structs
  • Use function pointers to select an operation at run time
  • Print aligned tables with field widths
  • Validate menu and table input

Features

  • Eight conversions between common units
  • Single-value conversion with two-decimal output
  • Conversion tables with a chosen start, step and number of rows
  • Absolute-zero warning for Kelvin conversions

Concepts demonstrated

  • Function pointers
  • Array of structs
  • Formatted output
  • Menu-driven design

System architecture

Each Conversion struct holds a label, the source and target unit names and a pointer to a double (*)(double) function. CONVERSIONS[] is the single source of truth: print_menu lists it, the menu choice indexes it, and print_table calls c->convert for each row.

Folder structure

TextProject tree
unit-converter/
├── README.md
├── Makefile
├── src/main.c
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.c
The conversion functions, the CONVERSIONS table, the menu, single conversions and print_table.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Unit Converter ===
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Celsius to Kelvin
4. Kilometres to miles
5. Miles to kilometres
6. Kilograms to pounds
7. Pounds to kilograms
8. Centimetres to inches
9. Print a conversion table
0. Exit
Choice:
Value in C:
37.00 C = 98.60 F
=== Unit Converter ===
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Celsius to Kelvin
4. Kilometres to miles
5. Miles to kilometres
6. Kilograms to pounds
7. Pounds to kilograms
8. Centimetres to inches
9. Print a conversion table
0. Exit
Choice:
Value in C:
-300.00 C = -26.85 K
Warning: below absolute zero - check the input.
=== Unit Converter ===
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Celsius to Kelvin
4. Kilometres to miles
5. Miles to kilometres
6. Kilograms to pounds
7. Pounds to kilograms
8. Centimetres to inches
9. Print a conversion table
0. Exit
Choice:
Conversion number (1-8):
Start, step, rows:
km | mi
-----------+-----------
0.00 | 0.00
5.00 | 3.11
10.00 | 6.21
15.00 | 9.32
20.00 | 12.43
=== Unit Converter ===
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Celsius to Kelvin
4. Kilometres to miles
5. Miles to kilometres
6. Kilograms to pounds
7. Pounds to kilograms
8. Centimetres to inches
9. Print a conversion table
0. Exit
Choice:
Value in mi:
26.20 mi = 42.16 km
=== Unit Converter ===
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Celsius to Kelvin
4. Kilometres to miles
5. Miles to kilometres
6. Kilograms to pounds
7. Pounds to kilograms
8. Centimetres to inches
9. Print a conversion table
0. Exit
Choice:
Bye!
Input
1
37
3
-300
9
4
0 5 5
5
26.2
0

Testing

  • Compare the scripted run with tests/expected-output.txt (37 °C, −300 °C → Kelvin warning, a km→mi table, a marathon in km→mi).
  • Check known values: 100 °C = 212 °F, 1 mi = 1.61 km, 1 kg = 2.20 lb.

Common errors

warning: initialization of 'double (*)(double)' from incompatible pointer type

A conversion function must take exactly one double and return double.

Table rows show 0.00 for every value

Read start and step with %lf, not %f or %d.

Possible improvements

  • Add currency conversion with rates read from a file
  • Support conversions in both directions from a single table entry
  • Group conversions into categories in the menu

Additional challenges

  1. 1Add volume units (litres, gallons) without changing any code except the table.
  2. 2Allow input such as 37C or 10km and detect the unit automatically.
  3. 3Print a two-column table converting in both directions.

Built and tested it yourself? Mark it to update your dashboard.