Skip to content

Instantly share code, notes, and snippets.

@skreuzer
Created February 19, 2014 15:38
Show Gist options
  • Save skreuzer/9094558 to your computer and use it in GitHub Desktop.
Save skreuzer/9094558 to your computer and use it in GitHub Desktop.
Temperature Conversion
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#define C2F(c) c * 9 / 5 + 32
#define F2C(f) (f - 32) * 5 / 9
int
main(int argc, char **argv)
{
int ch, c_flag = 0, f_flag = 0;
float temp;
while((ch = getopt(argc, argv, "fct:")) != -1) {
switch(ch) {
case 't':
temp = atof(optarg);
break;
case 'c':
c_flag = 1;
break;
case 'f':
f_flag = 1;
break;
}
}
if(c_flag == 1) {
printf("%.2fC = %.2fF\n", temp, C2F(temp));
}
if(f_flag == 1) {
printf("%.2fF = %.2fC\n", temp, F2C(temp));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment