scanf

scanf is used to get input from the user

The program

/* * File: cmtofeet.c * ---------------- * This program reads in a length given in * centimeters and converts it to its English * equivalent in feet and inches. */ #include <stdio.h> main() { /* Declare variables */ double totalInches, cm, inch; int feet; /* Explain program & get a length in */ /* centimeters from the user */ printf("This program converts cm to feet & inches.\n"); printf("Length in centimeters? "); scanf("%lf", &cm); /* Calculate totalInches and use this to */ /* calculate feet and inches */ totalInches = cm / 2.54; feet = totalInches / 12; inch = totalInches - feet * 12; /* Print results */ printf("%f cm = %d ft %f in\n", cm, feet, inch); }

The Output

lassie% cc cmtofeet.c lassie% lassie% a.out This program converts cm to feet & inches. Length in centimeters? 184 184.000000 cm = 6 ft 0.440945 in lassie% lassie% a.out This program converts cm to feet & inches. Length in centimeters? 178 178.000000 cm = 5 ft 10.078740 in lassie% lassie% a.out This program converts cm to feet & inches. Length in centimeters? 179 179.000000 cm = 5 ft 10.472440 in lassie% a.out lassie% a.out This program converts cm to feet & inches. Length in centimeters? 180 180.000000 cm = 5 ft 10.866141 in lassie%