2.3 STRUCTURE OF A C PROGRAM

Before we discuss about structure of a C program let us see a simple C program which does not perform any operation and only prints “Hello World” message:

After you compile and run this program it will print out the message: Hello World. This is the first C program we are exposed. Though the program is very simple, but you may learn a few points by going through eight lines of this program. #include< inserts another file. “.h”> is used for calling “header” files. These header file contain methods for C Programming Language performing needed tasks. All header files have interface to libraries and their cod for performing task is kept in other “.c” files.

Every C program contains a function called main. This is the starting point of the program. #include <stdio.h> is used to allow a program to interact with the screen as output on your computer. You will find it at the beginning of almost every C program.

The details of need of #include <stdio.h> we will discuss letter. The main() function declares the start of the program and the two curly brackets( { and }) show the start and end of the main function. Curly brackets in C are used to group statements in a block together to mark the beginning and end of a function or in the body of a loop( loop will be discussed later in this unit). This type of grouping is known as block in a program. 

printf(“Hello World\n”); prints the words on your computer screen. The text to be printed is enclosed in double quotes. The ‘\n’ at the end of the text tells the program to print a new line as part of the output.\n is used for the purpose of more clarity in the output. By putting \n at the end of the message to be printed it is ensured that when ever in a program some thing more will be printed on screen it will be printed in the next line. 

One more point you may note here is that C is case sensitive therefore it differentiate between a lower case letter and a upper case letter . For example main is different than Main. return 0 is written in this program for completing it. Because in int main( ) int means function main() returns an integer value, so there must be a statement that indicates what this int value is.The statement return 0 ; indicates that main() returns a value of 0 (zero) to the operating system(OS). If this program has completed successfully and terminated execution it will return 0 to OS. Here you do not have to know in detail about this concept. Without getting worried about this concept let us move on to know more about structure of a C program. But you just remember to use the return statement in your program when you write because main function always returns an integer.

In this program there are two functions: main and printf.

On the basis of our fist C program now let us see the basic structure of a C program which generally has following components:
1. program header comment
2. preprocessor directives (if any)
3. int main ( void )
4. {
5. statement(s);
6. return 0 ;
7. }

 

Now let us see the meaning of each line of this structure:

i) Program Header Comment:

A comment is description used for clarification. It helps a reader of the program understand the purpose or functionality of statements in the program. A C program may have several comments in whole program. It is good to have comment in the beginning of a program which state about the objective of the program for which it is used.

How to write a Comment?

In C all comments must begin with the characters /* and end with the characters */. For example if you write

/* This is my First C Program */

then statement “This is my First C Program” will be treated as comment. In any program the program header comment always comes first.

ii) Preprocessor Directives:

Preprocessor directives are special statements that is included in the program code as you have seen in section 2.3.1 of this unit. These directives are not program statements but special  instructions(directives) for the preprocessor and on the basis of these directives some activities are performed before actual program code compilation. For example #include <stdio.h> is called preprocessor directive (command).It directs the preprocessor to include all the functions defined in stdio.h in the program before it compiles the code written in the program. For more clarity please see the following prinf() statement:

printf(“Hello World\n”); statement in program given in previous page function printf( ) is defined in stdio.h and has been included in that program.

You remember that a preprocessor directive is written in only a single line of code. As soon as a newline character ‘\n’ is found, the preprocessor directive is considered to end. 

No semicolon (;) is used for the end of a preprocessor directive as in the case of other statement like: printf(“Hello World\n”); or return 0;.

In general “a preprocessor is a program which processes the input data and produce output that is used as input to another program”.

stdio.h – The #include <stdio.h> directive indicates the preprocessor to include a copy of the standard input/output header file stdio.h in the program code.

iii) int main ( void ): Generally every program has a function called main. This is the point from where program execution begins. The main () is placed in the source code file as the first function for the purpose of readability. In your program there must be a function with this name otherwise c compiler can not successfully compile and link your program.

Prototype for writing a function is:

return type function_name ( arguments)

A function may take some value for performing operations. The value(s) given to a function is known as arguments to the function. For example suppose you have written a function:

int sum( int a, int b)

for finding sum of two integers. This function return an integer and take two integers as argument it means you can pass two values of integers to the function sum. In side parenthesis ( ) after main, void is written, it means nothing is there inside ( ).

iv) The Function Body of main( ):The main function starts with a left brace (curly bracket){

begins the body of every function. A corresponding right brace } ends the function body. In side { and } are the statements to be performing the operation. For example if you are writing a program to find the area of a circle in C, then operations need to be performed for this purpose will be written inside {and }.

v) Statement(s): These are the main portion of any program. Statements written in a program decides about the activity to be performed such as input operations, output operations, logical operations etc.

vi) return 0; : Because function main() returns an integer value, there must be a statement that indicates what this value is. The last statement is return 0 ;. It ends with a semicolon. Also remember that all statements in C end with a semicolon. 

After learning about structure of a C program, now let us see the basic components of C programming. These components are used for writing C programs for solving problems. 

Next section is going to cover almost all the basic components of C programming. This section will give you idea for using basic components in the program you will write.


Comments

Popular posts from this blog

3.8 SECURE NETWORK DEVICES

3.5 SECURITY ISSUES FOR SMALL AND MEDIUM SIZED BUSINESSES

3.6 TOOLS FOR NETWORK SECURITY