0% found this document useful (0 votes)
5 views

C Structure

The document outlines the basic structure of a C program, which consists of six main sections: Documentation, Link, Definition, Global Declaration, Main Function, and Subprogram. Each section serves a specific purpose, such as providing program information, linking necessary libraries, defining constants, declaring global variables, executing the main logic, and containing user-defined functions. Examples are provided to illustrate the content and format of each section.

Uploaded by

ram726a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Structure

The document outlines the basic structure of a C program, which consists of six main sections: Documentation, Link, Definition, Global Declaration, Main Function, and Subprogram. Each section serves a specific purpose, such as providing program information, linking necessary libraries, defining constants, declaring global variables, executing the main logic, and containing user-defined functions. Examples are provided to illustrate the content and format of each section.

Uploaded by

ram726a
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Structure of a C program

The basic structure of a C program is given below. Any C program consists of 6 main sections.

S.N. Section Name Essential/ Optional

1 Documentation section Optional part

2 Link section Essential part

3 Definition section Optional part

4 Global declaration section Optional part


Function declaration section

5 Main function section Essential part

Sub-program section Optional part


6 Function 1
Function 2
…………..
Function N

1. Documentation Section
This section consists of comment lines which include the name of the program, the name of the
programmer and other details like time and date. Documentation section helps anyone to get an
overview of the program.

Example:
/* WAP to find the sum of 2 numbers
Author: Santosh Giri
Date: 01/01/2019
Time: 8 PM */

2. 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.

Example:
#include<stdio.h>
#include<conio.h>
3. Definition Section
All the symbolic constants are written in the definition section.

Example
#define PI 3.14

4. 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 (function prototyping).

Example:
float area; //global declaration section
void display(); //function prototyping

5. Main Function Section


It is necessary to have one main () function section in every C program from where the
execution of the program is started. 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 (;).
Example:
int main()
{
float r; //declaration part
printf("Enter value of r:"); //executable part start here
scanf("%f",&r);
area=PI*r*r;
printf("Area of the circle=%f \n",area);
display();
return 0;
}

6. Subprogram Section
The subprogram section contains all the user defined functions that are used to perform a specific
task. 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.

Example:
void display()
{
printf("This message is from Sub Function named display \n");
printf("we can take more Sub Functions \n");
}

Total Program
//documentation section
/* WAP to find the sum of 2 numbers
Author: Santosh Giri
Date: 01/01/2019
Time: 8 PM */

#include<stdio.h> //link section


#include<conio.h> //link section

#define PI 3.14 //definition section

float area; //global declaration section


void display(); //function prototyping

// main() program section


int main()
{
float r; //declaration part
printf("Enter value of r: "); //executable part start here
scanf("%f",&r);
area=PI*r*r;
printf("Area of the circle = %f \n",area);
display();
return 0;
}

//sub program section


void display()
{
printf("This message is from Sub Function named display \n");
printf("we can take more Sub Functions \n");
}
Output of the program:

Enter value of r: 3
Area of the circle = 28.260000
This message is from Sub Function named display
we can take more Sub Functions

You might also like