Every C program has a definite structure that is based on the basic structure of the C language. A typical C program mainly consist of 6 main sections as follows:
- Documentation Section
- Link Section
- Definition Section
- Global Declaration Section
- main() Function Section
- Sub Program Section
Without the above components the C program may not execute or compile properly.
Overview of typical C program structure
Documentation Section
Link Section
Definition Section
Global Declaration Section
main()
{
Declaration part
...
Executable part
...
}
Sub program Section
Function_1 ()
Function_2 ()
...
Function_n ()
1. The Documentation Section
The Documentation Section consists of a set of comment lines giving the name of the program and other details. Documentation section helps anyone to get an overview of the program. Comments are ignored by compiler and are used to provide documentation to people who reads that code. It consists of two type of comment system namely Single line comment and Multiline comment.
- The single line comment is created by using two forward slashes
//
- The multiline comment is created by using the combination
/* */
//Single line comment
/*
Multiline
comments
*/
2. The Link Section
The link section consists of header files that are processed before the compilation of the program by the program called as preprocesser. We use a preprocesser directive command to link the header files. The preprocesser directives commands begins with the hash symbol #
.
#include<filename.h>
3. Definition Section
All the symbolic constants are defined in the Definition Section. A symbolic constant is a constant value that remains unchanged throughout the program execution. A definition section also consist of the preprocesser directive command.
#define PI 3.14
4. Global Declaration Section
In global declaration section, global variables and user defined functions are declared. The variable declared in this section can be accessed and used anywhere in the program.
int radius;
5. main() Function Section
The mandatory part of every C program is the main() Function section. We can’t omit this section. The execution of the C program starts from this section and also ends in this section. The main() Function section consists of two parts:
- Declaration part
The declaration part consists of all the variables and functions declaration that will be used in the executable part.
- Executable part
This part contains the statements that are to be executed by the compiler. There should be atleast one executable statement in this part.
int main()
{
float rate; //Declaration part
int sum;
printf("Enter the rate: "); //Executable part
scanf("%f", &rate);
printf("Enter the sum: ");
scanf("%d", &sum);
printf("The rate is : %f \n", rate);
printf("The sum is : %d \n", sum);
return 0;
}
The main() Function section must have opening and closing braces { }
. Every statements inside main() function must be terminated with semicolon ;
except for some exceptions.
6. Sub program section
Subprogram section is consists of all the user-defined functions. It is not compulsary to have the sub program section. A complete and fully functional C program can also be created by omitting this section. A sub program section generally occurs after the main() function section.
void example()
{
printf("This is a sub program.");
}
A typical example of C program having all the sections
//A program to calculate area and perimeter of circle
#include<stdio.h>
#define PI 3.14
float radius;
int main()
{
float area;
void perimeter(void);
printf("Enter the radius of the circle: ");
scanf("%f",&radius);
area = PI*radius*radius;
printf("Area is = %f ", area);
perimeter();
return 0;
}
void perimeter()
{
float per;
per = 2*PI*radius;
printf("\nPerimeter is = %f",per);
}