22 October 2012

Part 2 - Hello, World and Fahrenheit-Celsius conversion

Topics Covered

1. The main() function
2. Return codes and command-line arguments
3. Fahrenheit-Celsius conversion

Watch Video Part 2a >>


Watch Video Part 2b >>

Downloadable Files




Make the following modifications to fahr.c:
1. The program as written uses integer calculations, limiting its accuracy. Change to use floating-point calculations and output. Print out at least 2 decimal places for all temperatures.
2. Display the output in a two-column table with a heading at the top. Make the decimal points in the output align vertically.
3. Modify the program to accept command-line arguments of the form fahr.exe LOWER UPPER STEP. For example, running the command
fahr 0 150 20.5
should produce a table with temperatures going from 0.0 to 150.0 in steps of 20.5. Use the function atof("123.456") to convert a string "123.456" into a floating-point value.
4. Try running the modified program from Step 3 with a variety of possibilities for LOWER, UPPER and STEP. Does it still run correctly if you leave off some of the arguments?


What is the value of the expression 5/9 after it is assigned to an integer variable? Write a short program to answer this question. Compare your answer with the following alternatives:
100 * (5 / 9)
(100 * 5) / 9
100 * (5.0 / 9.0)
100.0 * (5 / 9)

Quick reference

Function signature

A function signature defines the return type as well as the number and type of input arguments. For example, the function signature for the main() function is: int main(int, char*[])

Printf format strings

The printf function requires different format strings depending on the type of arguments you give it. Some common ones are: %d (to print an int), %s (to print a char*) and %f (to print a double).


This variable is set by the Windows Command Prompt after running a program. To view its contents, use the command echo %errorlevel%


Kernighan and Ritchie. The C Programming Language.

No comments:

Post a Comment