Source One Educational Society    
Home | OS Install | MS-Office | Photoshop | SQL | Networking | HTML | Java | Model Papers | FAQ's | Health Tips | News | Jobs | Fun SMS
C - Tutorial
INTRODUCTION
o Getting Started
o C Boot Disk
o What Is An Identifier
o Getting Started C
PROGRAM CONTROL
o While Loop
o Do-While Loop
o For Loop
o If Statement
o Break And Continue
o Switch Statement
o Goto Statement
o Finally, A Meaningful Program
ASSIGNMENT & LOGICAL COMPARES
o Integer Assignment Statements
o Additional Data Types
   
   
 
 
C Language Tutorial
Program Control  

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.

 
Another Poor Programming Example

Recalling uglyform.c from the last chapter, you saw a very poorly formatted program. If
you load and display dumbconv.c you will have an example of poor formatting which is much closer to what you will actually find in practice. This is the same program as tempconv.c with the comments removed and the variable names changed to remove the descriptive aspect of the names. Although this program does exactly the same as the last one, it is much more difficult to read and understand. You should begin to develop good programming practices now.

{
int x1,x2,x3;
printf("Centigrade to Farenheit temperature table\n\n");
for(x1 = -2;x1 <= 12;x1 = 1){
x3 = 10 * x1;
x2 = 32 + (x3 * 9)/5;
printf(" C =%4d F =%4d ",x3,x2);
if (x3 == 0)
printf("Freezing point of water");

if (x3 == 100)
printf("Boiling point of water");
printf("\n");
}
}

Compile and run this program to see that it does exactly what the last one did.

 
Programming Exercises
  1. Write a program that writes your name on the monitor ten times. Write this program three times, once with each looping method.
  2. Write a program that counts from one to ten, prints the values on a separate line for each,
    and includes a message of your choice when the count is 3 and a different message when the
    count is 7.

 

 
 
Quick Links
Java FAQ's
XML FAQ's
.Net FAQ's
Javascript FAQ's
SQL Interview Questions
 
 
 
 
Home  |  Aboutus  |  Post Your Content  |  Tell a Friend  |  Contact us  |  Sitemap
© 2009 365edu. All Rights Reserved. Site by 365edu. Best view in 1024x768 resolution.