summaryrefslogtreecommitdiff
path: root/src/ctof.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ctof.c')
-rw-r--r--src/ctof.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/ctof.c b/src/ctof.c
new file mode 100644
index 0000000..e61cfcd
--- /dev/null
+++ b/src/ctof.c
@@ -0,0 +1,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;
+}