Basic Structure of C Language Program

All programming languages follows a well-defiend structure, including C language. In this tutorial, we are going to study about the structure of a C Language program with examples.


A Program, written in C Language can be divided into six sections: Documentation, Link, Definition, Global Declaration, Main() Function, and Subprograms. Dividing a whole program into different-different section, increases the readability of the program and makes it easier to modify & document it. Lets discuss each and every section in detail.

1. Documentation Section

The details associated with the program are given in the documentation section. This section generally contains the description of the program, programmer's name, and creation date. This section provides an overview of the program to the reader.

C
/**
 * file: average.c
 * author: hashcodec
 * description: C Program to find average of two numbers.
 */

A Header file is a collection of built-in functions, which we can directly use in our program. To use any of the standard functions, the appropriate header file must be included. This section is used to declare or import all the header files, which will be used in program. During compilation, these header files are included in the program.

C
#include <stdio.h>

3. Definition Section

This section is used to define different-different constants, using #define keyword. During compilation, It replaces all the constants with its value in the code.

C
#define NUM1 2000;
#define NUM2 3000;

4. Global Declaration Section

This section includes declaration of global variables, function, static global variables and static global functions. All the global variables, which needs to be referenced throughout the file, can be declared here.

C
int findAverage(int num1, int num2);

5. Main Function Section

Main function is the first function of every C program that is responsible for starting the execution and termination of the program. It is mandatory to include a main() function in every C program. The return type of main function can be either int or void.

C
int main()
{
  printf("Average: %d", findAverage(NUM1, NUM2));
  return 0;
}

6. Sub Program or User Defined Functions Section

This section contains all the user defined functions. The functions, which are defined here can be used in main section. Dividing a large program into smaller functions is always a good practice.

C
int findAverage(int num1, int num2) {
    return (num1 + num2)/2;
}

Basic C Language Program Example:

C
//Documentation Section
/**
 * file: average.c
 * author: hashcodec
 * description: C Program to find average of two numbers.
 */

//Link Section
#include <stdio.h>

//Definition Section
#define NUM1 2000;
#define NUM2 3000;

//Global Declaration Section
int findAverage(int num1, int num2);

//Main() Function Section
int main()
{
  printf("Average: %d", findAverage(NUM1, NUM2));
  return 0;
}

//Subprograms Section
int findAverage(int num1, int num2) {
    return (num1 + num2)/2;
}