Structure of C Program
Structure of C Program
Structure of C 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.
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.
main( ) Every C program must have a main() function which is the starting point of the
{ program execution.
#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
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.