twitter
    Find out what I'm doing, Follow Me :)

Finally, A Meaningful Program

Load the file named tempconv.c for an example of a useful, even though somewhat limited program. This is a program that generates a list of centigrade and Fahrenheit temperatures and prints a message out at the freezing point of water and another at the boiling point of water.

/***********************************************************/
/* This is a temperature conversion program written in */
/* the C programming language. This program generates */
/* and displays a table of farenheit and centigrade */
/* temperatures, and lists the freezing and boiling */
/* of water */
/***********************************************************/
main( )
{
int count; /* a loop control variable */
int farenheit; /* the temperature in farenheit degrees */
int centigrade; /* the temperature in centigrade degrees */
printf("Centigrade to farenheit temperature table\n\n");
for(count = -2;count <= 12;count = count + 1 ){
centigrade = 10 * count;
farenheit = 32 + (centigrade * 9)/5;
printf(" C =%4d F =%4d ",centigrade,farenheit);
if (centigrade == 0)
printf(" Freezing point of water");
if (centigrade == 100)
printf(" Boiling point of water");
printf("\n");
} /* end of for loop */
}



Of particular importance is the formatting. The header is simply several lines of comments describing what the program does in a manner the catches the readers attention and is still pleasing to the eye. You will eventually develop your own formatting style, but this is a good way to start.

Also if you observe the for loop, you will notice that all of the contents of the compound statement are indented a few spaces to the right of the "for" reserved word, and the closing brace is lined up under the "f" in "for". This makes debugging a bit easier because the construction becomes very obvious. You will also notice that the "printf" statements that are in the "if" statements within the big "for" loop are indented three additional spaces because they are part of another construct. This is the first program in which we used more than one variable. The three variables are simply defined on three different lines and are used in the same manner as a single variable was used in previous programs. By defining them on different lines, we have opportunity to define each with a comment.