
Basic Structure of C Programming: A Comprehensive Guide
Table of Content:
- Basic Structure of C Program
- Documentation Section
- Link Section
- Definition Section
- Global Declaration Section
- main() Function Section
- Subprogram Section
- Total Program
- Total Program without sub function
- Total Program with sub function
- Related Questions
- Related Assignment
- Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.
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 . . .
- Question 1: Which compiler switch to be used for compiling the programs using math library with gcc compiler?
- Question 2: Can the curly brackets { } be used to enclose a single line of code?
- Question 3: What are header files and what are its uses in C programming?
- Question 4: What is syntax error?
- Question 5: Can two or more operators such as and be combined in a single line of program code?
- Question 6: What are compound statements?
- Question 7: What is wrong in this statement? scanf("%d",whatnumber);
- Question 8: What are comments and how do you insert it in a C program?
- Question 9: What are reserved words?
- Question 10: Not all reserved words are written in lowercase. TRUE or FALSE?
- Question 11: Why is it that not all header files are declared in every C program?
- Question 12: What are run-time errors?
- Question 13: What is the difference between functions abs() and fabs()?
- Question 14: What is the general form of a C program?
- Question 15: What is the use of a semicolon (;) at the end of every program statement?
- Question 16: What are all the sections that a C program may have and must have?
- Question 17: What is the use of printf() and scanf() functions?
- Question 18: Can we compile a program without main() function?
Related Questions
- Assignment 1: Program to show swap of two no's without using third variable
- Assignment 2: Program to find gross salary
- Assignment 3: C program to convert centimeter to Inches