Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

convertf

License: MIT Language: C

A small, dependency-free C library for converting between common units — 108 functions across 7 categories.

double miles = cf_km_to_mi(42.195); // 26.22

Contents

Supported units

Category Units
Length km m cm mm mi ft in
Mass kg g lb oz
Temperature c f k
Volume l ml gal (US) cup (US)
Time s min h day
Area m2 km2 ft2 acre
Velocity kmh ms mph kt

Every unit in a category converts to every other unit in that category

Naming convention

Every function is cf_<from>_to_<to>, takes a double, and returns a double:

cf_km_to_mi(double km);   // kilometers  -> miles
cf_c_to_f(double c);      // Celsius     -> Fahrenheit
cf_kt_to_kmh(double kt);  // knots       -> km/h

Build

Requires gcc and make.

make build      # compiles cf.c, archives it into libconvertf.a
make install    # installs the lib + header under /usr/local (override with PREFIX=)
make clean      # removes build artifacts

Usage

#include "cf.h"
#include <stdio.h>

int main(void) {
    double miles = cf_km_to_mi(42.195); // marathon distance
    printf("%.2f km is %.2f mi\n", 42.195, miles);
    return 0;
}

Link against the static library once built:

gcc myprogram.c -L. -lconvertf -o myprogram

Precision

Conversion factors are given to 5-6 significant figures, so results are accurate to within roughly 0.1% of the true value, fine for everyday use, not intended for scientific-grade precision work.

License

MIT. See LICENSE for details.

Releases

Packages

Contributors

Languages