C - SPLessons

Chapter 4

Structure of C Program

Structure of C Program

“Structure of C Program” chapter gives clear explanation of various sections of C-programs.

Any program is written and executed in various steps to give a desired output.Even if a small program is coded, it requires a structure.

Structure

Any C program need to follow a structure which consists of certain rules. The Structure of C Program can be as follows which has various sections.Some of the sections are essential and some of them are optional.

Sections in Structure of C Program
Documentation Section
Link Section
Definition Section
Global Declaration Section
Main Function Section
Sub-programming Section

Documentation Section:

Documentation section is used for giving the comments regarding the title, author,more detailed information about the program etc.

Eg:
Single line comments

//sum  of two numbers

Multi-line comments

/*Welcome to SPLessons
You are reading C-programming tutorial
This program is about summing of two numbers*/

 

Link section is used to link the compiler to the library function using header files.

Eg:

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

Definition Section:

Definition section consists of symbolic constants.

Eg:

#define true 1

Global Declaration Section:

The variables used anywhere in the program are called as global variables. In this section, global variables are declared which can be also used in user-defined functions.

Eg:

int x=10;

Main Function Section:

Main function section is the main part of the program. It mainly consists of two parts which are written between the braces {} and every line have to end with a semicolon(;).

Declaration Section: In declaration section, all the required variables are declared which can be used in the executable part.

Executable Section: In executable section, statements are given which are helpful in executing the program and giving the output.

Eg:

{
Function 1
Function 2
.
.
}

Sub-Programming section:

Sub-programming section consists of user-defined functions.These functions are called by the main function section.

/*C basic structure example
to find sum of two numbers */	//documentation section

#include<stdio.h>		//Link section

int total;			//Global declaration section
int sum(int,int);		//Function declaration section 
void main()			//Main section
 {
  int a,b;
  printf("\n Enter the two numbers : ");
  scanf("%d  %d",&a,&b);    /* taking two numbers as input*/
  total = sum(a,b);      /* calling function.The value returned by the function is stored in total */
  printf("\n The sum of two numbers is : %d ",total);
  getch();
 }

 int sum ( int num1,int num2)	//User defined section 
 {
  int result;  		/* defining variable, its scope lies within function */
  result = num1 + num2 ;   /*adding two numbers*/
  return (result) ;		//definition section
 }

Output:

Enter the two numbers : 1 2

The sum of two numbers is : 3

All the rules should be followed in every section to get a successful output from a program.

Summary

  • C-Structure is built-up based on 6 sections.
  • Execution starts from main section.
  • Sub-programming section and definition sections are optional.

Using documentation section(comments) is the best practice because it explains the program clearly .