Sunday, June 23, 2013

C is Structural Programming language , Why ?

C is called structure programming language , because it is strictly restricted to its sructure . anything out of its structure is not accepted .

Structure of C

Header File
Sub Function Declaration
Global variable
main Function Definition
{
Local variable
Content of Main Function
}
Sub Function Definition
{
Local variable
Content of Sub Function
}

Example

#include<stdio.h>
#include<conio.h>
int sum;
void sum(int c,int d);
void main()
{
int a=10,b=20;
clrscr();
sum(a,b);
printf(" The Sum is %d ",sum);
getch();
}
void sum(int c,int d)
{
sum=c+d;
}

The data type int, printf are defined in STDIO header file . STDIO  stands for Standard input and output .
clrscr(); getch() are defined in CONIO header file . CONIO  stands Console Input Output . the Variable SUM is known as Global variable and its scope (lifetime) is throughout program . where as variable a and b is local variable its scope(lifetime) is with that function only . so  in the above Program SUM is used in sub function and Main fuction .

Out of Structure Program 

The program which doesn't follow  the above structure is not accepted by c compiler , it will show an error .
the example of out of structure program is shown below

#include<stdio.h>
#include<conio.h>
void sum(int c,int d);
void main()
{
int a=10,b=20,sum;
clrscr();
sum(a,b);
int s;
s=sum;
printf(" The Sum is %d ",s);
getch();
}
void sum(int c,int d)
{
sum=c+d;
}

In the above program  variable Sum is used in both main as well as subfunction , so it is Global variable , so it should defined below the header file, the variable c is declared in the middle of the program but as per  C structure it should be declared in the beginning . so when above code is compiled by c compiler it show an error

Like the Post? Do share with your Friends.

No comments:

Post a Comment