Explain the basic structure of c program with an example

It’s essential for all of student for you before proceeding to learn more advanced lessons of C programming. For this reason in this part we will discuss about the basic structure of c program .This also called the general structure of c program.


Explain the basic structure of c program with an example


Basic structure of c Program

Example

Documentation section (Used for comments )

//simple program created by jannat

Link section

#include <stdio.h>

#include<conio.h>

Definition section

Void main()

Global Declaration section

(Variable used in more then one function )

Void add();

Int x = 100;

Main() function section

{

Declaration part

Execution part

}

Main()/entry point

{

Int a = 10,b=20;

Add(a,b);

Printf(“the value of sum inside main : %d”,sum)

}

Subprogram section

[User-define function ]

Function 1 ..

Function 2 ..

..

 

Function n

Void add(int a,int b)

{

Int sum = a+b;

Return sum;

 

}

 

“In the basic structure of c “ C programming has the following sections -

1. Documentations or documentation section

2. Pre-Processor statements or Link Sections.

3. Definition section

4. Global Declarations section

5. Main Function (local Declarations, program statements and Expressions)

6. Subprogram section

 

See a program to understand easily the explanation of a basic structure of c program

 

/**

*File Name : helloword.c

*Author : Muminul Islam Akib

*Date : 10/23/2020

*Description : a program to display hello word

*no input needed

*/ (comments)

 

#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;

}

 

Documentation section :

The documentation section is the part of the program where the programmer gives the details associated with the program.This is basically consist of plain text written in English and gives some information about the written program. It usually gives the name of the program, the details of the author and other details like the time of coding and description. It gives anyone reading the code the overview of the code. This section is included inside comments.

 

For example -

/**

*File Name : helloword.c

*Author : Muminul Islam Akib

*Date : 10/23/2020

*Description : a program to display hello word

*no input needed

*/

 

Link section -

This part of the code is used to declare all the header files that will be used in program. This leads to the compiler being told to link the header files to the system. It provides instruction to the compiler to link function from the library function.

It means The link section is used to include the external header files in which the per-defined functions are available. To use the per-define functions in our programs, these files must be linked with our program. File can be included by using “include” directive.

 

For example - #include<stdio.h>

            #include<conio.h>

 

In the above example, “stdio.h” is a header file which is available in the system area in the programmer’s computer. The angular brackets “< > “ tells the compiler to search the file “stdio.h” in the  system area.

 

If we specify the above examples as #include <stdio.h>, then the compiler searches for the file “stdio.h” in the current directory.

 

Definition section -

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

 

For example - #define PI = 3.14

 

In the above code we have created a constant PI and assigned 3.14 to it .

The #define is a preprocessor compiler directive which is used to create constant.we generally use uppercase letters to create constants. 

 

The #define is not a statement and must not end with a semicolon(;) .

 

Global declaration section -

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

 

float area(float r)

float r = 10;

 

Main function section -

Every C program need 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.


For example -

 

 int main(void)

{

  float r = 10;

  printf("Area: %.2f", area(r));

  return 0;

}

 

 

Subprogram

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”

 

In a short view -

 

Explain the basic structure of c program with an example
 

 

Post a Comment

0 Comments