
In this article, you will learn about the basic structure of a C program. A C program consists of different functions; here we will define the necessary functions that are needed in the basic structure of a C program.
All C programs have sections/parts, which are mentioned below.
- Documentation section
- Link Section
- Definition section of Basic Structure of C Program
- Global declaration section
- Function prototype declaration section
- Main function
- User-defined function definition section
1. Documentation section:
In the basic structure of a 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. Link section:
The link section provides instructions to the compiler to link functions from the system library, such as using the include directive.
3. Definition section of the basic structure of C Program
The definition section defines all symbolic constants using the #define directive.
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
5. Main () function section:
Every C program must have one main function section. This section contains two parts: the declaration part and the 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 pass semicolon. |
6. Subprogram 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 the basic structure of a C program are very important to understand, so keep focus firstly on these basic sections.
7. User-Defined Function
Users can define their own functions in this section, which perform particular tasks as per the user requirement. So users create this according to their needs.
Example
The C program below is a very simple and basic program in the C programming language. This C program displays “The sum of two integer numbers given by the user,” enters numbers in the main function, calls the user-defined function, and returns the answer after processing.
The C language is case sensitive so each statement should be ended with a semicolon, which is a statement terminator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/*
Documentation section
C programming basics & structure of C programs
Author: gillanidata.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:
Enter number 1 and number 2
20 30
Sum of two numbers: 50 |
You can also check the prime number function to understand the basic structure.
You can also check the table of a number entered by the user in C.