Basic Structure of C Program

Every C program is basically a group of different section that are used for different purpose.

A well-defined structured layout makes program more readable, easy to modify, consistent format and self-documented.

Basic structure of C program is shown in following Figure:

Different section of C programs

  1. Documentation Section: Documentation section is generally description of program. Documentation section of c programming consists program's name, program's objective, author name and creation date etc.
    Example:
    
    /* This program calculates area of circle
    Author: Ramesh Bhandari
    Date: November 27, 2017
    */
    
  2. Header File Section: This section includes all necessary header files which are required for the working of different function in program.
    Example:
    
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
  3. Definition Section: This section is used to define symbolic constants. Generally capital letter is used to define symbolic constants.
    Example:
    
    #define PI 3.141592
    #define SIZE 10
    #define GOLDENRATIO 1.618
    
    Here PI, SIZE and GOLDENRATIO are symbolic constants.
  4. Global Variable and Function Prototype Section: This section is used to declare global variables. Global variables are generally used in more than one function. Similarly all prototypes of sub-program (user defined functions) are declared in this section.
    Example:
    
    /* Global Variables */
    int a;
    float b;
    char c;
    /* End of Global Variables */
    /* Function Prototype Section */
    int sum(int x, int y);
    void message( void );
    /* End of Function Prototype Section */
    
  5. Main Function Section: Every C program must have main() function. It consists two parts, namely, local variable declaration section and executable statement section, and these two parts must be in between opening and closing braces.

    Local Variable Declaration Section: This section is used for declaring local variables which are to be used in executable statement part.
    Example:
    
    int x;
    float y;
    char z;
    

    Executable Statement Section: Executable statement section consists all the instructions to be executed. The program execution starts from opening brace of main() function and ends at closing brace of main() function. The closing brace of main() function is logical end of program.
  6. Sub Program Section: This section consists definition of all user defined function that are declared in function prototype section.

Note: All of the section except main() section may be absent when they are not required. main() is the unique section of C program from where execution starts. Use of more than one main() is not allowed.