Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Structure of C Program

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Any C program is consists of 6 main sections. Below you will find brief explanation of each of them.

Basic Structure of C Program


Documentation Section
This section consists of comment lines which include the name of 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.

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.

Definition Section
All the symbolic constants are written in definition section. Macros are known as symbolic constants.

Global Declaration Section


The global variables that can be used anywhere in the program are declared in global declaration
section. This section also declares the user defined functions.

main() Function Section


It is necessary 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
program starts at opening braces and ends at closing braces.

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.
Following is the basic structure of a C program.
Documentation Consists of comments, some description of the program, programmer name and
any other useful points that can be referenced later.

Link Provides instruction to the compiler to link function from the library function.

Definition Consists of symbolic constants.

Global Consists of function declaration and global variables.


declaration

main( ) Every C program must have a main() function which is the starting point of the
{ program execution.

Subprograms User defined functions.

Let’s explore the sections with an example.


Write a program to print area of a circle.
In the following example we will find the area of a circle for a given radius 10cm.
Formula
The formula to compute the area of a circle is πr2 where π is PI = 3.1416 (approx.) and r is the radius
of the circle.
Lets write the C code to compute the area of the circle.
/**
* file: circle.c
* date: 2010-11-25
* description: program to find the area of a circle
* using the radius r
*/

#include <stdio.h>
#define PI 3.1416
float area(float r);
int main(void)
{
float r = 10;
printf("Area: %.2f", area(r));
return 0;
}

float area(float r) {
return PI * r * r;
}
The above code will give the following output.
Area: 314.16

Different sections of the above code


Documentation
This section contains a multi line comment describing the code.
/**
* file: circle.c
* date: 2010-11-25
* description: program to find the area of a circle
* using the radius r
*/
In C, we can create single line comment using two forward slash // and we can create multi line
comment using /* */.
Comments are ignored by the compiler and is used to write notes and document code.

Link
This section includes header file.
#include <stdio.h>
We are including the stdio.h input/output header file from the C library.

Definition
This section contains constant.
#define PI 3.1416
In the above code we have created a constant PI and assigned 3.1416 to it.
The #define is a preprocessor compiler directive which is used to create constants. We generally use
uppercase letters to create constants.
The #define is not a statement and must not end with a ; semicolon.

Global declaration
This section contains function declaration.
float area(float r);
We have declared an area function which takes a floating number (i.e., number with decimal parts)
as argument and returns floating number.

main( ) function
This section contains the main() function.
int main(void)
{
float r = 10;
printf("Area: %.2f", area(r));
return 0;
}
This is the main() function of the code. Inside this function we have created a floating variable r and
assigned 10 to it.
Then we have called the printf() function. The first argument contains "Area: %.2f" which means we
will print floating number having only 2 decimal place. In the second argument we are calling the
area() function and passing the value of r to it.
Subprograms
This section contains a subprogram, an area() function that is called from the main() function.
float area(float r) {
return PI * r * r;
}
This is the definition of the area() function. It receives the value of radius in variable r and then
returns the area of the circle using the following formula PI * r * r.

You might also like