Basic Structure of ‘C’ Program | What is the Basic Structure of C Program?

Every C program is basically a group of different section that is used for different purpose. The structure gives us a basic idea of the order of the sections in a program. A well-defined structured layout makes program more readable, easy to modify, consistent format and gives better clarity and the concept of a program. Whenever we write a program in ‘C’ language, we can divide that program into six different sections. These sections are as follows:

  • Documentation section
  • Linkage section
  • User definition section
  • Global declaration section
  • Main() function section
  • Subprogram section


Pictorial Representation of Basic Structure of C Programming:



Figure: Basic Structure of C Program


A program in ‘C’ may not contain all the sections shown above but main() function is a must as the program execution always starts with main() . 


Brief explanation of each section is given below:


Documentation Section:

Documentation section is generally description of program. It is only a set of comments for program documentation. Documentation section consist program’s name, objective of the program, author’s name and other useful details like the creation date, title etc. that can be referenced later. It provides an overview of the program.


Example

/*

Author: Eliza Begum

Date: 2/2/2022

Description: This program calculate simple interest

*/


Linkage Section: 

The linkage section consists of the header files that are used in the program. It provides instruction to the compiler to link function from the system library. If the programmer wants any one of the functions, from any one of the header files, there should be a mechanism to inform the compiler. From the link section, we instruct the compiler to link those header files from the system libraries, which we have declared in the link section in our program.


Example:

#include<stdio.h>

#include<conio.h>

#include<string.h>


User Definition Section:

This section is used to define symbolic constants. Symbolic constants are used for better understanding of certain names that are not going to change throughout the program. Generally capital letter is used to define symbolic constants. The keyword define is used in this part. Macros are used in this section.


Example:

#define PI 3.1415

#define MAX 5

Here, PI, MAX are symbolic constants.


Global Declaration Section: 

The global variables that can be used anywhere in the program are declared in global declaration section. Global variables can be used by all the functions. The user-defined functions are also declared in this part. The user defined functions specified the functions specified as per the requirements of the user.


Example:

int sum(int x,int y);

int a;

float b;


Main() Function Section: 

The main() function has control over the program. Every C program must have a main function which is the starting point of the program execution. It contains 2 parts, namely, local variable declaration part and Executable statement part and these two parts must be written in between opening and closing braces. Local variable declaration part is used for declaring local variables which are to be used in executable statement part. Executable statement contains all the instructions to be executed.


int main()

{

int a=5;                   // Declaration Part

printf(“a=%d”,a);     // Execution Part

return 0;

}


Sub-Program Section: 

The subprogram section contains all the user defined functions that are used to perform a specific task. All the function subprograms are user defined with an optional list of arguments enclosed in parentheses and generally placed after the main() definition is over. There can be placed in any order.


int sum(int a, int b)

{

return a+b;

}





Post a Comment

0 Comments