c-programming

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 00a927d51f767a45f4e9fa752cc6ab49c830789f
parent 2bb437333b8f37d898cf46bc5adedcd11c891d1c
Author: Andrew Laack <andrew@laack.co>
Date:   Fri, 17 Oct 2025 22:52:51 -0500

Fahrenheit with symbolic constants

Diffstat:
Ac-book/ch1/fahrenheit2.c | 44++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+), 0 deletions(-)

diff --git a/c-book/ch1/fahrenheit2.c b/c-book/ch1/fahrenheit2.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> + +#define FALLBACK_LOWER 0 +#define FALLBACK_UPPER 100 +#define FALLBACK_STEP 10 + +float celsius(int fahr){ + return (fahr - 32) * (5.0 / 9.0); +} + + +int main(int argc, char** argv){ + + + int lower, upper, step; + + lower = FALLBACK_LOWER; + upper = FALLBACK_UPPER; + step = FALLBACK_STEP; + + if (argc >= 2){ + lower = atoi(argv[1]); + } + if (argc >= 3){ + upper = atoi(argv[2]); + } + if (argc >= 4){ + step = atoi(argv[3]); + } + if (argc >= 5){ + printf("Too many arguments. Usage: fahr {lower} {upper} {step}\n"); + return -1; + } + + if (step <= 0){ + printf("Positive steps only\n"); + return -1; + } + + for(int fahr = lower; fahr < upper ; fahr += step){ + printf("%5d %6.1f\n", fahr, celsius(fahr)); + } +}