envirementalb Logo
envirementalb Logo

Basic Structure of C Program

In this article, you will learn about basic structure of C program. C program consists of different function here we will define necessary function that are needed in basic structure of c program.
All C programs are having sections/parts which are mentioned  below.

Basic Structure of C Program

Sections of the C Program

    1. Documentation
    2. Preprocessor Section
    3. Definition
    4. Global Declaration
    5. Main() Function
    6. Sub Programs

1. Documentation section

In the basic structure of c program the documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later.

2. Preprocessor Section

All the header files of the c program are declared in the preprocessor section of the program. Header files access the library codes that are necessory for our program. Multiple files is inserted into program before the process compilation. 

Example:

#include<stdio.h>
#include<math.h>

3. Definition section

The definition section defines all symbolic constants such using the #define directive.

Example:

#define long long ll


4. Global declaration section


There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the user-defined functions.

Example:

int num = 18;

5. Main () function section

Every C program must have one main function section. This section contains two parts; declaration part and executable part.

A. Declaration part: The declaration part declares all the variables used in the executable part.
B. Executable part: There is at least one statement in the executable part. These two parts must appear between the opening and closing braces. The program execution begins at the opening brace and ends at the closing brace. The closing brace of the main function is the logical end of the program. All statements in the declaration and executable pas semicolon.

6. Sub Program Section


If the program is a multi-function program then the subprogram section contains all the user-defined functions that are called in the main () function. User-defined functions are generally placed immediately after the main () function, although they may appear in any order.All above section of basic structure of c program  are very important to understand so keep focus firstly on these basic sections.

Example:

void main()
or 
int main()

 

Below C program is a very simple and basic program in C programming language. This C program displays “The sum of Two integer numbers given by the user, user entered numbers in main function and call the user defined function and user defined function return the answer after processing.
C language is case sensitive so each statement should be ended with semicolon (;) which is a statement terminator.

/*
    Documentation section
    C programming basics & structure of C programs
    Author: envirementalb.com
    Date : 28/09/17
    Update:29/09/17
*/
#include <stdio.h>   /* Link section */
int total = 0;       /* Global declaration, definition section */
int sum (int, int);  /* Function declaration section */
int main ()          /* Main function */
{
int a,b,total; /*Local variable declaration*/
    printf (“Enter number1 and number2”); /*output function*/
    scanf(“%d %d”,&a,&b); /*input function*/
total = sum (a,b); /*function call */
printf (“Sum of two numbers : %d \n”, total);
    getch();
}
int sum (int a, int b) /* User defined function */
{  
return a + b;      /* definition section */
}

Output

Output Will according to the input numbers

Steps involved in the Compilation and execution of a C program

  • 1st of all Program Creation
  • Second Compilation of the program
  • 3rd Execution of the program
  • 4th utput of the program