Structure of C program

  • Documentation Section
  • Include Section
  • Global Declaration Section
  • Main Function Section
  • Declaration part
  • Executable part
  • User Defined Section

Example :

/* Program to find addition and subtraction of Two Numbers */
 #include<stdio.h> 
int a=10,b=5;/* Global variable declaration*/
void sub(); 
void main() 
{ int x,y,z; /* variable declaration*/
printf("Enter x and y values");
scanf("%d%d",&x,&y);/* predefined input function*/ 
z=x+y; 
printf("%d",z); 
sub();/* function call*/ 
} 
void sub()/* user defined function*/ 
{ int c; c=a-b; 
printf("%d",c);/* predefined output function*/ 
}

Documentation Section :


This Section contain details of the Program. It is also known as Comment line, it will help us know the  details of the code, we can write comment line any where in the program .

Example /* variable declaration*/
        /* predefined input function*/

 

include Section:

It contains predefined header files. In above example #include<stdio.h> is header file. Header files contains predefined  functions ( also Known as library functions) in above example we used scanf() and printf() library functions.whenever we use library functions we must include corresponding header file in the include action.

Following are some header files.

#include<stdio.h>  library fucntions like scanf() and printf() etc.,
#include<conio.h>  library fucntions like clrscr() and getch() etc.,
#include<string.h> library fucntions like scanf() and printf() etc.,
#include<math.h>   library fucntions like math() and sin() etc.,

Global Declaration Section 

This section contains global variables declarations and user defined functions declarations .

in above example

 int a=10,b=5;/* Global variable declaration*/

here a and b are global variables and

void sub();

is user defined function declaration.

Global Variables

Variables which are declared outside of the main function are called Global variables. We can use this variables throughout the program.

Main Function Section

main() function is mandatory for all C programs, program execution always starts from main function. Main function section contains two parts

  1. Declaration Part
  2. Executable Part.

Declaration Section

This section contains all the list of variables used in the program, in this section we have to declare variables with corresponding data type.

in above example

int x,y,z; /* variable declaration*/

here x,y,z are local variables

Local Variables: Variables which are declared with in the functions are called local variables, scope of variables is with in the function Only.

Executable part

This part contains Executable statements , which are excitable by the compiler

in above example

 printf("Enter x and y values");
 scanf("%d%d",&x,&y);
 z=x+y; printf("%d",z);

are executable statements .

User Defined Section

User defined function is a function which is defined by the user. it contains own logic of the function.

 

Related Posts