Code Example:/* MODULE: main.c DESCRIPTION:* Program that takes a temperature from the user on the command line, then* displays that temperature as celsius converted to Fahrenheit, and* as Fahrenheit converted to celsius./#include #define iARGS_REQUIRED 2#define iARG_EXE 0#define iARG_INPUT 1static floatfCelsiusToFahrenheit( float fCelsius );static floatfFahrenheitToCelsius( float fFahrenheit );/* MAIN/intmain( int iArgc, char *acpArgv[] ){ float fFahrenheit = 0.0; float fCelsius = 0.0; float fInput = 0.0; /* user didn't provide a temperature on the command line */ if(iArgc != iARGS_REQUIRED) { fprintf(stderr, "Usage: %s [temperature]\n", acpArgv[iARG_EXE]); return 0; } /* read the given temperature into the fInput variable */ sscanf(acpArgv[iARG_INPUT], "%f", &fInput); /* fInput treated as celsius and converted to Fahrenheit */ fFahrenheit = fCelsiusToFahrenheit(fInput); /* fInput treated as Fahrenheit and converted to celsius */ fCelsius = fFahrenheitToCelsius(fInput); printf( "%.2f degrees Fahrenheit is %.2f degrees celsius.\n", fInput, fCelsius ); printf( "%.2f degrees celsius is %.2f degrees Fahrenheit.\n", fInput, fFahrenheit ); return 0;}/* STATIC FUNCTION: fCelsiusToFahrenheit DESCRIPTION:* Converts a celsius temperature to Fahrenheit. PARAMETERS:* fCelsius: The temperature in celsius to convert. RETURNS:* fCelsius converted to Fahrenheit./static floatfCelsiusToFahrenheit( float fCelsius ){ return (fCelsius * 1.8) + 32;}/* STATIC FUNCTION: fFahrenheitToCelsius DESCRIPTION:* Converts a Fahrenheit temperature to celsius. PARAMETERS:* fFahrenheit: The temperature in Fahrenheit to convert. RETURNS:* fFahrenheit converted to celsius./static floatfFahrenheitToCelsius( float fFahrenheit ){ return (fFahrenheit - 32) / 1.8;}
Copyright © 2026 eLLeNow.com All Rights Reserved.