Basic Structure of C Programming
Table of Content:
Any C program is consists of 6 main sections. Below you will find a brief explanation of each of them.
Pictorial Representation of Basic Struture of C Programming
Basic Structure of C Program
- Documentation Section
- Link Section
- Definition Section
- Global Declaration Section
- main() Function Section
- Subprogram Section
Pictorial Representation of Basic Struture of C Programming essention section and optional and suggested
Pictorial Representation of pramming parts of Basic Struture of C Programming
Documentation Section
This section consists of comment lines which include the name of the program, the name of the programmer, the author and other details like time and date of writing the program. Documentation section helps anyone to get an overview of the program.
/*Documentation Section: Program Name: Program to find the area of circle Author: Rumman Ansari Date : 12/01/2013 Time : 10 AM */
Link Section
The link section consists of the header files of the functions that are used in the program. It provides instructions to the compiler to link functions from the system library such as using the #include directive.
#include"stdio.h" //link section #include"conio.h" //link section/
Definition Section
All the symbolic constants are written in the definition section. Macros are known as symbolic constants.
#define PI 3.14 //definition section
Global Declaration Section
The global variables that can be used anywhere in the program are declared in the global declaration section. This section also declares the user defined functions.
float area; //global declaration section
main()
Function Section
It is necessary to have one main() function section in every C program. This section contains two parts,
declaration and executable part. The declaration part declares all the variables that are used in executable part.
These two parts must be written in between the opening and closing braces. Each statement in the declaration
and executable part must end with a semicolon (;
). The execution of the program starts at opening braces and
ends at closing braces.
- Declaration part: The declaration part declares all the variables used in the executable part.
- 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 part end with a semicolon.
void main() { float r; //declaration part printf("Enter the radius of the circle\n"); //executable part start here scanf("%f",&r); area=PI*r*r; printf("Area of the circle=%f \n",area); message(); }
Subprogram Section
The subprogram section contains all the user defined functions that are used to perform a specific task.
These user defined functions are called in the main()
function.
If the program is a multifunction program then the sub program 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.
void message() { printf("This Sub Function \n"); printf("we can take more Sub Function \n"); }
Total Program
/*Documentation Section: Program Name: Program to find the area of circle Author: Rumman Ansari Date : 12/01/2013 Time : 10 AM */ #include"stdio.h" //link section #include"conio.h" //link section/ #define PI 3.14 //definition section float area; //global declaration section void main() { float r; //declaration part printf("Enter the radius of the circle\n"); //executable part scanf("%f",&r); area=PI*r*r; printf("Area of the circle=%f \n",area); message(); } void message() { printf("This Sub Function \n"); printf("we can take more Sub Function \n"); }
Enter the radius of the circle 3 Area of the circle=28.260000 This Sub Function we can take more Sub Function Press any key to continue . . .
Total Program without sub function
/*Documentation Section: Program Name: Program to find the area of circle Author: Rumman Ansari Date : 12/01/2013 Time : 10 AM */ #include "stdio.h" //link section #include "conio.h" //link section #define PI 3.14 //definition section float area; //global declaration section void main() { //declaration part float r; //executable part starts here printf("Enter the radius of the circle\n"); scanf("%f",&r); area=PI*r*r; printf("Area of the circle=%f \n",area); getch(); }
Output
Enter the radius of the circle 3 Area of the circle=28.260000 Press any key to continue . . .
Total Program with sub function
/*Documentation Section: Program Name: Program to calculate sum of two Number Author: Rumman Ansari Date : 12/01/2013 Time : 10 AM */ #include/* Link section */ int total=0 ; /* Global declaration, definition section */ int sum(int,int); /* Function declaration section */ int main() /* Main function */ { printf("C programming basics & structure of C programs \n"); total=sum(6,6); printf("sum=%d\n",total); } int sum(int a, int b) /* User defined function */ { return a+b; }
Output
C programming basics & structure of C programs sum=12 Press any key to continue . . .