I have started to learn C and I have tried my hand at temperature conversion. The idea is to convert a temperature from Fahrenheit to Celsius and vice/versa. Can somebody review my code and let me know how I might be able to improve it? My program is as follows:
#include <stdio.h>
void fahconv()
{
double fah;
puts("Enter value to convert from Fahrenheit to Celsius");
scanf("%lf",&fah);
printf("Converted temperature from F to C: %lf",((fah - 32.0)*5.0)/9.0);
}
void celsconv()
{
double cels;
puts("Enter value to convert from Celsius to Fahrenheit");
scanf("%lf",&cels);
printf("Converted temperature from C to F: %lf",(cels*9/5)+32);
}
void invalidchoice()
{
printf("Invalid choice..exiting");
}
int main()
{
char choice;
puts("Enter (F or f) to convert from Fahrenheit to Celsius");
puts("Enter (C or c) to convert from Celsius to Fahrenheit \n");
scanf("%c",&choice);
switch(choice)
{
case 'c':
case 'C': celsconv();
break;
case 'f':
case 'F': fahconv();
break;
default: invalidchoice();
}
return 0;
}
fahconv
vs ints incelsconv
is distracting and worries that reader that it might be significant. \$\endgroup\$cels
is declared a double. \$\endgroup\$