1.4 Structure of C Programs
Every C program consists of one or more functions. A function is subroutine that may include one or more statements designedto perform a specific task. One of the functions must be called "main" and program will always begin by main function,which may access other functions. The general C structure is as follows.
Documentation Section
Link Section
Definition Section
Global Declaration Section
Main() Function Section
{
Declaration Section
Executable Section
}
Sub Program Section or User Defined Functions
1. Documentation Section:
In this section we sepcify the purpose Of the program. This portion has only meaning for the programmer. For example /* Program to find area of circle */.These are also known as comments or remarks in the C
language. The portion written in between /* and */ is invisible for the compiler.
2. Link Section:
For example:
if we are using printf function in our program then will link stdio.h header file to our program as #include<stdio.h>.printf and scanf functions are available in this library.
Note:
In C some library files are automatically link to the program so sometimes we need not to specify this portion in the program. C compiler automatically links stdio.h and conio.h header files.
3. Definition Section:
This is also known as preprocessor block of the program. We may use this block to define constant terms used in the program.
For example: #define PI 3.14
This statement defines constant PI with value 3.14. We may also specify macros in this section.
4. Global Declaration Section:
Global variables are those variables which are used in the more than one function. All the global variables used in the program are defined in this section.Variables defined in this section can be accessed from any user defined function as well as main function of the program.
5. main( ) Function:
This is very important function of any c program. Every C program must have one and only one main()function. Every C program's execution starts from this function and ends with this function. After compilation of first four blocks compiler starts compilation from this function. Every C function has two blocks namely declaration block and execution block. All the variables used in the function must be declared in the declaration block of the function. Execution block contains all the commands (executable statements) based on the logic of the program. These two parts must be enclosed with in the curly braces { and }. The opening brace { indicates the beginning of program execution and closing brace } indicates the logical end of the program.
6. Sub Program Section:
It contains all the user defined functions that are called in the main function. Generally these functions are placed after the main function although they may appear in any order.
0 Comments