STRUCTURE OF C PROGRAM


C Program can be viewed as a group of building blocks called functions. A Function is a single instruction or set of instructions designed to perform specific task. A Function is a subroutine that may include one or more statements or instructions designed to perform a specific task.

à ‘C’ Program contains the following sections.

ü  Documentation Section
ü  Link Section
ü  Definition Section
ü  Global Declaration Section
ü  main() function Section
ü  Subprogram Section


Figure  Structure of C Program


Documentation Section
It consists of a set of comment lines which contains user interested details like name of author, program and any other details which makes the program more easy to understand.
            Comment lines should be enclosed within /* and */  for multiple lines and // for single line and can appear anywhere in the program. These comment lines are omitted by the compiler at the time of compilation.
Eg:-
             /* Welcome to
             Or blog
            */  
            // Welcome

Link Section
C has a rich set of built-in functions which can be used to write any complex program. These standard functions are kept in various system libraries such as stdio.h (standard input-output header file), math.h (mathematical functions header file), ctype.h (character testing and conversion functions header file), string.h (string manipulations header file), conio.h (configuration input-output header file) etc. If we want to use the functions of these libraries, we have to provide instructions to the compiler to link the functions from the corresponding system library. This can be achieved through link section of the ‘C’ program structure by using #include directive.
Eg:-
#include<stdio.h>
            #include<math.h>
            #include<conio.h>

Definition Section
Some programs require constant values which are going to be used more than once with in the program. Small changes in that value requires multiple changes in the program. Such complications can be avoided using symbolic constants and these can be defined in definition section using “#define” directive.
Eg:-  #define   pi   3.14




Global Declaration Section
There may be some variables that are used in more than one function. Such variables are called global variables and are declared in global declaration section i.e., outside of all the functions in general these variables are declared above main() function. This section also declares all user-defined functions.

main( ) function Section
main( ) indicates starting of the program. Every ‘C’ program must have one main( ) function section. This section contains two parts.
  • Declaration Section
  • Executable Section
Declaration part declares all the variables used in the executable part and the entire code for the program is written in the executable part. These two parts must appear between the opening and closing braces. The program  execution begins at the opening brace and ends at the closing brace. All the statements in declaration part and executable part end with a semicolon (;).
Syntax:-
            main()
            {                     
            Declaration Part;
            Executable Part;
}
Subprogram Section
This section can consists of one or more user defined functions. These functions can be called from main( ) function or any other user-defined function. User-defined functions are placed immediately after the main( ) function and they may appear in any order.
           

Note:- All sections except the main( ) function section may be absent whey they are not required.

No comments:

Post a Comment