Discuss structure of a C Program with suitable example.

This answer is restricted. Please login to view the answer of this question.

Login Now

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

Basic Structure of C Program
Documentation Section
Link Section
Definition Section
Global Declaration Section
main() Function section

{

Declaration Part 1

Execution Part

}

Subprogram Sections

Function 1

Function 2

Function n

1. Documentation Section

Here we can see two types of comments in the above program. Comments are the explanation or description of source code that does not affect your program logic in any way. Comments are neglected by compilers or interpreters.

// This is single line comments

/*
*    This
*        is
*          the multiple line comment.
*/

2. Link Section

This part of the code is used to declare all the header files that will be used in the program. This leads to the compiler being told to link the header files to the system libraries.

#include<stdio.h>

3. Definition Section

In this section, we define different constants. The keyword define is used in this part.

#define PI 3.14

4. Global Declaration Section

This part of the code is the part where the global variables are declared. All the global variable used are declared in this part. The user-defined functions are also declared in this part of the code.

float area(float r);
int a=7;

5. Main Function Section

Every C-programs needs to have the main function. Each main function contains 2 parts. A declaration part and an Execution part. The declaration part is the part where all the variables are declared. The execution part begins with the curly brackets and ends with the curly close bracket. Both the declaration and execution part are inside the curly braces.

int main()
{

    //statements

}

6. Sub Program Section

All the user-defined functions are defined in this section of the program.

float area(float r)
{
     return PI * r * r;
}

#Sample Program

The C program here will find the area of a circle using a user-defined function and a global variable pi holding the value of pi

//a program to calculate area of circle

/** link section */
#include<stdio.h>

/** defination section */
#define PI 3.142 

/** Global Declaration */
float area(float r);

/** main function */
int main(){
	float r;
	printf("Enter the radius: ");
	scanf("%f", &r);
	printf("Area of Circle = %f ", area(r));
	return 0;
}

/** sub program */
float area(float r){
	return PI * r * r;
}
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .