Write about storage classes in C?


Scope: the region of the program in which a vars. Is available for use.

Visibility: The program ability to access a variable form the memory
 
Life time: the life time of a variable is the duration of time in which a variable exists in the memory during execution.

Storage class: The area or scope of a variable where and how it is declared is called as storage class.

Storage class of the variable tells the compiler:

Storage area of the variable

Initial value of the variable, if not initialized. i.e default initial value.

Scope of the variable, in which functions the value of a variable could be available.

Life times of the variable i.e. how long the variable could be active in the program.

In C all variable must have a data type and they also have a storage class. The storage classes are

1. Automatic variables

2. External variables

3. Static variables

4. Register variables

1. Automatic variables : 

Automatic variables are declared inside a fns in which they are to be utilized.

They are created when the function is called and destroyed automatically when the function is existed.

Automatic variables are also called local or internal variables.

A variable declared inside a function, without storage class specification is by default an automatic variable.

Ex: int no;  ( or)  auto int no;

Features of automatic storage class:

1. Storage - Memory

2. Default initial value - Garbage

3. Scope - local to the block in which the variable is defined.

4. Life time - Till the control remains within the block in which the variable is defined.

Ex: 

void fn1 ( void);
void fn2 ( void );
main ( )
{
int m = 1000;
fn2( );
printf ( “% d \n “, m);
}

void fn1 ( void)
{
int m =10;
printf ( “%d \n “, m);
}

void fn2 ( void )
{
int m =100;
fn1( );
printf ( “%d \n “, m);
}

2. External variables:

variables that are both alive and active throughout the entire program are known as external variables. They are also known as global variables. Global variables can be accessed by any function in the program. External variables are declared outside a function.

Ex: 

int count;
main ( )
{
count = 10;
}
fn ( )
{
int count = 0;
count = count + 1;
}

When the function references the variable count, it will be referencing only its local variable, not the global one. The value of count in main will not be affected.

Features of external storage class:

1. Storage - memory

2. Default initial value - 0

3 .Scope - Global

4. Life time - as login as the program execution does not comes to end.

Ex: 

void display ( );
int x;
main ( )
{
int x = 20;
printf ( “%d in “ x);
display ( );
}

void display ( )
{
x = x+10;
printf ( “\n %d”,x);
}

3. Static Variables:

The value of static variables persists until the end of the program.

A variable can be declared as static using the keyword static like:

static int x ;

static float y;

A static variable may be either an internal type or an external type depending on the place of declaration.

Internal static variable are those which are declared inside a function. The scope of internal static variables extends up to the end of the function in which they are defined.

 Internal static variables are similar to auto variables, except that they retain in existence (alive) throughout the remainder of the program.

 Internal static variables can be used to retain values between function calls. (It can be used to count the number of calls made to a function.

An external static variable is declared outside of all functions. And is available to all the functions in that program.

Features of static storage class:

Storage - Memory

Default initial value - 0

Scope - Local to the block in which variable is defined.

Life time - Static variables retain in existence throughout the entire program execution comes to end.

Ex: 

void start ( void);
main ( )
{
int i;
for ( i =1; i  < =3; i ++)
start ( );
}

void start ( void)
{
static int x = 0;
x = x+1;
printf ( “x=%d \n “x);
}

If we declare x as an auto variables the out put would have been

x =1
x =1
x =1

Because each time start is called, the auto variable x is initialized to zero. When the function terminates, its value of 1 is lost.

4. Register variables : 

We can toll the compiler that a variable should be keep it in one of the machine’s registers. Instead of keeping in the memory (where normal variables are stored).

Since a register accesses is much faster than a memory access, keeping the frequently accented variables (loop control variables) in the register will lead to faster execution of programs. This is done as follows:

register int count;

Features of register storage class:

Storage – CPU registers

Default initial value  - garbage

Scope – local to the block in which the variable is defined.

Life time –Till the control remains within the block in which the variable is defined.

Related

C Language 3955961047291507345

Post a Comment

emo-but-icon

item