/    /  Structure of C Program

Structure of C Program

 

The basic structure of the C Program consists of 6 sections. Each section has its own significance and is meant to perform a specific task.

 

Structure of C Program


Documentation Section
:

 

This section provides information about the program, the author who has written the program, the date and time when the program has been written, the file name of the program, and other necessary information.

Documentation is presented in comments which can be a single line comment or multiline comments and it is not considered for the compilation process.

 

Example:

 

/* Write a Program to print the sum of two numbers Written by John
On 23-oct-2016
File Name : sum.c */

 

Link Section:

 

This section primarily links all the necessary header files “.h” or user- written program files “.c, .txt, etc..” to the main program. Linkers will link up all the required header files to the program. We need to include the necessary header files for standard input or output related functions in the program.

 

Examples:

 

# include <stdio.h>
# include <conio.h>
# include <strings.h> for strings
# include <math.h> for mathematical manipulation # include <stdlib.h> for library functions

 

Definition Section:

 

This section mainly defines symbolic constants in the program. The values declared here will not change throughout the program. When the compiler identifies the symbolic constants in the main program, the value defined to it will be automatically replaced with that variable. The scope of these values will be throughout the program.

 

The syntax for defining symbolic constants:

 

# define PI 3.14.

 

Examples:

 

# define PRINCIPLE 2000
# define MIN_MARKS 40 
# define MAX_MARKS 99

 

Declaration Section:

 

This section is mainly used to declare global variables, function declarations, structures, and unions. These can be used throughout the program.

 

Example:

 

 int sum(int a, int b);

 

Main Section:

 

It is the heart of the program. It gives information to the compiler that the program is started at main() and every program should have only one main function. It consists of a declaration that deals with local variables and execution that deals with the logic of the program.

 

main()
{
int a=5, b=4, c; 
c=sum(a,b);
printf(“sum of %d and %d= %d”, a,b,c);
}

 

Sub Program Section:

 

User-defined programs are represented within this section and are referred to as functions.

 

int sum(int x, int y)
{
int z; 
z=x + y; 
return z;
}