0% found this document useful (0 votes)
2 views4 pages

5.program Structure

The document explains the concept of variable scope and storage classes in programming, particularly in C. It details four types of storage classes: automatic, external, static, and register, along with their characteristics and examples. It also provides skeletal structures and sample programs demonstrating the use of user-defined functions with different storage classes.

Uploaded by

Mhřžñ Řk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

5.program Structure

The document explains the concept of variable scope and storage classes in programming, particularly in C. It details four types of storage classes: automatic, external, static, and register, along with their characteristics and examples. It also provides skeletal structures and sample programs demonstrating the use of user-defined functions with different storage classes.

Uploaded by

Mhřžñ Řk
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Program Structure

In earlier chapters, we've used local variables, which have scope within
the single function in which the variable is defined. Such variable is not
recognized in other functions. However, the same name is used in other
functions; it is required to re-define the variable. In other words, it reserves
different memory for both the variables however same name is used.
But, in some situations, it may require to define a variable in such a way
so that the scope of variable remains more than one function or throughout the
program from the point of its definition.

So, permanence of a variable and its scope within the program is


characterized with Storage Class. It shows the portion of program over which the
variable is recognized.
Hence, we can say that a variable has two characteristics, one data type
and another storage class.
The four types of storage class are –
automatic, external, static and register which can be defined using
keywords auto, extern, static and register respectively. Some typical variables
can be declared as follows:

auto int a,b,c;


extern char name[10];
static int x;

Automatic variable
Automatic variables are always declared within a function and its scope retains
within the function only in which they are declared. So, same name can be used
as variables in different functions and different memories are allocated for them.
Hence such variables are also known as local variables. While declaring
automatic variable within a function, it is not required to use auto keyword i.e.
auto is optional.

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

float quad(int a, int b,int c, int i)


{
float x=(-b+i*(sqrt(pow(b,2)-4*a*c)))/(2*a);
return x;
}

void main()
{
clrscr();
auto int a=4,b=5,c=1;
float x,y;

x=quad(a,b,c,1);
y=quad(a,b,c,-1);

printf("\n %f",x);
printf("\n %f",y);

getch();
}

External variables
The scope of external variable extends from the point of definition throughout the
remainder of program. Since the variable is recognized throughout the program
from the point of declaration, it retains its values. Hence, external variable can be
assigned a value within a function and can be used within another function. It
provides a convenient way for transferring information back and forth between
functions without using arguments. One more, it can return more than one data
items from a function without using RETURN statement.

/* To find real roots of quadratic equation */


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

int a=4,b=5,c=1;

float quad(int i)
{
float x=(-b+i*(sqrt(pow(b,2)-4*a*c)))/(2*a);
return x;
}

void main()
{
clrscr();

float x,y;

x=quad(1);
y=quad(-1);

printf("\n %f",x);
printf("\n %f",y);
getch();
}
/* To find real root of quadratic equation */

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

extern int a=4,b=5,c=1;

float x,y;

void quad()
{
x=(-b+(sqrt(pow(b,2)-4*a*c)))/(2*a);
y=(-b-(sqrt(pow(b,2)-4*a*c)))/(2*a);

void main()
{
clrscr();
quad();

printf("\n %f",x);
printf("\n %f",y);

getch();
}

Static Variable
Static variable is defined within individual functions and therefore has the
same scope as automatic variable. So, it is similar to automatic variable i.e. local
to the current function in which it is defined. But, Static variable retains its
previous values throughout the life of program however the function (in which
static variables are defined) is re-called after exit.
Static variable is defined in the same way as automatic variable with
keyword static before data type of variables which shows static storage class.

#include<stdio.h>
#include<conio.h>
long int fact(int i)
{
static long int x=1;
return(x*=i);
}

void main()
{
long int y;
int a=6;
for(int i=1;i<=a;i++)
y=fact(i);

printf("\n %ld",y);
}

It's unusual to define automatic or static variable having the same names as
external variables. If happened, local variables will take precedence over the
external variables. But it doesn't affect the values of external variables.

Here is one skeletal structure of a C program showing various storage class.

float a,b,c;

main()
{
static float a;
void dummy(void);
....
}

void dummy(void)
{
static int a;
int b;
........
}

Write programs using UDF(user defined function) different storage class.


1. to convert a character to uppercase using UDF.
2. to determine the greater number between two input integer numbers.
3. To check an input number is prime or composite
4. to find factorial of N.

You might also like