Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Basic Structure of a C Program

Basic Structure of C Program

A C Program has the following entites that define structure of the program –

  1. Preprocessor Commands
  2. Functions
  3. Variables
  4. Statements & Expressions
  5. Comments

Structure

Let’s take an example for exploring these sections:

In this example we will write a program to find the area of a circle with radius = 5.

/* Comments
   Name of Developer    - Somya Jain
   Program - To find the area of a circle using radius*/

//Preprocessor section
//May include libraries, macros etc
#include <stdio.h>
#define PI  3.1416
float area (float r);

int main(void)
{
  //Example of variables
  float r = 5;
 printf("Area:  %.2f",area(r));

  return 0
}

//Example of function
float area(float r)
{
  //example of statements & Expression
  return  PI * r * r;
}

Output:

Area: 78.54

Basic structure of C language –

Documentation Section:-

The C program is divided  into several sections, the Documentation section, which is at the top of the program. In this section, programmer can provide information related to this section like-

  • who created the program.
  • what is the purpose of the program
  • Which date the program was created.
  • Any necessary information related to the program can be written here.
/* Name    - Somya Jain
Program-  To find the area of a circle using radius r
Created -  12/7/19
Place     - Noida */

Link Section:-

In this Link section, programmers can capture external files in their current programs.In which the header file is primarily linked. Header files are a type of file.

In this section we write all the header files, inside which we have to use the functions with the use of in the next program. 

Like:-

#include<stdio.h> Because of this, we use a function like printf(), and scanf().

#include<conio.h> Because of this, we use a function like clrscr(), and getch().

#include<stdio.h>
#include<conio.h>

Definition Section:-

In this section we define a symbolic constant that we have to use in the entire program, here the value that is defined once does not change during the entire program. These symbolic constants are also called micro.

#define PI  3.1416

Global declaration section:-

In this section we define a symbolic constant that we have to use in the entire program. Whenever a program requires a variable that can be accessed by any function of the program, then the programmer should define such a variable in the global declaration section.

This section contains function declaration:

float area (float r);

Main() function Section:-

Main function is the most important section in C programming.C program is not run without main function, so whenever a program is created in the C language, it must have a main function.which are written in the main function section.whenever you implement a program built in C, the compiler automatically executes the main function and if the program does not have a main function,then the program warns the programmer by displaying the error message. 

Within the main function,there are two sub parts:

  • Declaration part
  • Executable part

In declaration part we defines the local variable.which we have to use inside our main function.while the executable part is the part of the main function where the program processes or executes the data provided.

This part contains the main function:

int main(void)
{
float r = 5;
printf(“Area:  %.2f”,area(r));
return 0;
}

Sub-Program Section:-

If you want to use a user defined function in C program, then the user defined function is defined in the same section. Sub-program section below the main function section.here you can define user defined function.

This section contains sub program:

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