summaryrefslogtreecommitdiff
path: root/src/c/temp-converter/ctof.c
blob: e61cfcd29a2c4c1f470b609952417ddfdc32e64e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>
#include <errno.h> // for errno
#include <limits.h> // for INT_MAX, INT_MIN


/*
    F = C x 9/5 + 32
*/

int
main(int argc, char *argv[])
{

    if (argc < 2) {
        printf("Usage: %s number\n", argv[0]);
        return 1;
    }

    char *endptr;

    errno = 0; // reset errno

    long value = strtol(argv[1], &endptr, 10); // base 10

    if (endptr == argv[1]) {
        printf("Error: No digits were found\n");
        return 1;
    }
    if (*endptr != '\0') {
        printf("Warning: Trailing characters after number: %s\n", endptr);
        return 1;
    }
    if ((value == LONG_MAX || value == LONG_MIN) && errno == ERANGE) {
        printf("Error: Number out of range\n");
        return 1;
    }

    float fah = value * 9/5 + 32;

    printf("Fahrenheit: %0.f\n", fah);

    return 0;
}