Structure of a C Program

·

The structure of a C program can by explained by taking an example of a C program to calculate area and perimeter of a rectangle. The program is written as follows.

/* Example program */
/* This program finds the area and perimeter of a rectangle */

# include"stdio.h"
main( )
{
int p,q, area, perimeter;
p = 4
q = 6
area = p*q;
perimeter = 2 * (p+q);
printf(“area = %d\n”, area);
printf(“perimeter = % d\n”, perimeter);
}
/* End of main */

Comments
In the above program the first line in the program starts with /* and ends with */. Any thing written between /* and */ is called a comment. In the C Language comments are an aid to the programmer to read and understand a program. It is not a statement of the language. The compiler ignores comments It is a good practice to include comments in a program which will help in understanding a program.

Now let us observe the line
# include "stdio.h"

This is called a preprocessor directive. It is written at the beginning of the program. It com- mands that the contents of the file stdio.h should be included in the compiled machine code at the place where # include appears. The file stdio.h contains the standard input/output rou- tines. All preprocessor directives begin with pound sign # which must be entered in the first column. The # include line must not end with a semicolon. Only one preprocessor directive can appear in one line.

Main( ) Function
The next line is main( ). It defines what is known as a function in C. A C program is made up of many functions. The function main( ) is required in all C programs. It indicates the start of a C program. We will use main( ) at the beginning of all programs. Observe that main( ) is not followed by a comma or semicolon.

Braces {And}
Braces{and} enclose the computations carried out by main ( ). Each line in the program is a statement. Every statement is terminated by a semicolon; The statement itself can be written anywhere in a line. More than one statement can be on a line as a semicolon separates them. However it is a good practice to write one statement per line.

Declaration
In the above program the first statement int p, q, area, perimeter;
This statement is called a declaration. It informs the compiler that p,q, area and perimeter are variable names and that individual boxes must be reserved for them in the memory of the computer. Further it tells that the data to be stored in the memory boxes named p,q, area and perimeters are integers namely, whole numbers without a fractional part(e.g, 0,1,2,...). The statement ends with a semicolon The effect of statement is shown in figure given below;









Fig.Effect of defining p, q, area and perimeter as integer variable names.

Assignment Statements
The statement p=4; is an assignment statement. It commands that the integer 4 be stored in the memory box named p. when the statement is executed the integer 4 will be stored in the memory box named p as shown in fig. This statement assigns a value 4 to the variable name p.





The next statement q=6; assigns 6 to q.

Arithmetic Statement
The statement area=p*q; is an arithmetic statement. It commands that the numbers stored in memory boxes p and q should be copied in to the CPU. The original contents of p and q remain in their respective boxes. These numbers are multiplied by the CPU and product is stored in box named area. After executing this statement the box named area will contain 24 as shown in fig.








The next statement perimeter = 2 *(p+q) is also an arithmetic statement.





The next two statements in the program are commands to display the contents of memory
box named area and perimeter respectively. The library function used for display is printf( ). the general form of this function is

printf (format string, variable 1, variable2, ——variable n);

The format string is enclosed in quotes. It specifics any message to be printed and the man- ner in which the contents of variables are to be displayed for each of the variables in the list of variables.

printf(“area = % d\n”, area);

The format string is % d\n The symbol % d says interpret the variable area occurring after the comma in the printf statement as an integer and display its value”. The symbol \n causes the display to advance to the next line. Thus when this statement is carried out we will see on the screen
area = 24
After the statement
printf(“perimeter = % d\n”, perimeter);
we will have on the screen area = 24
perimeter = 20

About Me

Blog Archive