BASIC STRUCTURE OF C PROGRAMS :
Structure of C program is defined by set of rules called protocol,
to be followed by programmer while writing C program. All C
programs are having sections/parts which are mentioned below.
1. Documentation section
2. Link Section
3. Definition Section
4. Global declaration section
5. Function prototype declaration section
6. Main function
7. User defined function definition section
EXAMPLE C PROGRAM TO COMPARE ALL THE SECTIONS:
You can compare all the sections of a C program with the below C program.
1 /*
2 Documentation section
3 C programming basics & structure of C programs
4 Author: fresh2refresh.com
5 Date : 01/01/2012
6 */
7
8 #include <stdio.h> /* Link section */
9 int total = 0; /* Global declaration, definition section */
10 int sum (int, int); /* Function declaration section */
11 int main () /* Main function */
12 {
13 printf ("This is a C basic program \n");
14 total = sum (1, 1);
15 printf ("Sum of two numbers : %d \n", total);
16 return 0;
17 }
18
19 int sum (int a, int b) /* User defined function */
20 {
21 return a + b; /* definition section */
22 }
OUTPUT:
This is a C basic program
Sum of two numbers : 2
DESCRIPTION FOR EACH SECTION OF THE C PROGRAM:
● Let us see about each section of a C basic program in detail below.
● Please note that a C program mayn’t have all below mentioned sections
except main function and link sections.
● Also, a C program structure mayn’t be in below mentioned order.
Sections Description
We can give comments about the program,
creation or modified date, author name etc
in this section. The characters or words or
anything which are given between “/*” and
“*/”, won’t be considered by C compiler for
compilation process.These will be ignored
by C compiler during compilation.
Documentation Example : /* comment line1 comment
section line2 comment 3 */
Header files that are required to execute a
Link Section C program are included in this section
In this section, variables are defined and
Definition Section values are set to these variables.
Global variables are defined in this section.
When a variable is to be used throughout
Global declaration the program, can be defined in this
section section.
Function
prototype Function prototype gives many information
declaration about a function like return type,
section parameter names used inside the function.
Every C program is started from main
function and this function contains two
major sections called declaration section
Main function and executable section.
User can define their own functions in this
User defined section which perform particular task as
function section per the user requirement.