Sunday, 17 September 2017

Storage Classes in C

Today, we are learning Storage classes in C. Storage classes actually tells us that where the variable would be stored, it's initial value and it's scope and life.

There are four storage classes in c :
  1. Automatic storage class
  2. Register storage class
  3. Static storage class
  4. External storage class

Storage Classes In C

Automatic storage class:

All the local variable is by default auto. An "auto" is a keyword for automatic variables. we can also define local variable as automatic variable explicitly by using "auto" keyword.

Storage- RAM
Initial value- Garbage value
Scope- Local to the block in which the variable is defined.
Life- Till the control remains within the block in which the variable is defined.

Let us take an example for understanding auto keyword.

#include<stdio.h>
void main()
{
  auto int i,j;
  printf("%d  %d\n",i,j);
 }

OUTPUT:
4583453  3485735

Explanation: When we run this program , we can get different values because it is nothing but garbage values. So always be remember to initialize the automatic variable properly, otherwise you will get garbage value as the output of the given program.

Register Storage Class:

Storage- CPU Registers
Initial value- Garbage value
Scope- Local to the block in which the variable is defined.
Life- Till the control remains within the block in which the variable is defined.

A value stored in a CPU register can always be accessed faster than the one that is stored in memory.
Therefore, if a variable used at many places in a program, it is better to declare its storage class as register (for example - loop counter variables).

Let us take an example of Register Storage class:

#include<stdio.h>
void main()
{
  register int i;
  for(i=1;i<=10;i++)
    {
      printf("%d\n",i);
   }
}

Explanation: It's output is 1 to 10 but it is not mandatory that the value of i would be stored in a CPU register. Do you know the reason for this?
Because the number of CPU registers are limited, and they may be busy doing some other task. So then that variable works as if its storage class is auto.

Static Storage Class:

Storage − Memory.
Default initial value − Zero.
Scope − Local to the block in which the variable is defined.
Life − Value of the variable retains between different function calls.

Now, we are trying to understand static storage classes by two programs(program 1 and program 2).

Program 1:

void main( )
 {
counter( ) ;
counter( ) ;
counter( ) ;
 }
void counter( )
 {
 int i = 1 ; // auto variable
printf ( "%d\n", i ) ;
i = i + 1 ;

 }

OUTPUT:
1
1
1

Program 2:


void main( )
 {
counter( ) ;
counter( ) ;
counter( ) ;
 }
void counter( )
 {
 static int i = 1 ; // static variable
printf ( "%d\n", i ) ;
i = i + 1 ;

 }

OUTPUT:
1
2
3

Like auto variables, static variables are also local to the block in which they are declared. The difference between them is that static variables don’t disappear when the function is no longer active. Their values retains. If the control comes back to the same function again the static variables have the same values they had last time around.

External Storage Class:

Storage − Memory.
Default initial value − Zero.
Scope − Global.
Life − As long as the program’s execution doesn’t come to an end.

External variables are declared outside all functions, yet are available to all functions that care to
use them.

int x = 21 ;
void  main( )
 {
  extern int y ;
  printf ( "\n%d %d", x, y ) ; 
  } 
   int y = 31 ;

Here, x and y both are global variables. Since both of them have been defined outside all the functions both enjoy external storage class. 
extern int y ;
int y = 31 ;
Here the first statement is a declaration, whereas the second is the definition. When we declare a variable no space is reserved for it, whereas, when we define it space gets reserved for it in memory. We had to declare y since it is being used in printf( ) before it’s definition is encountered.

No comments:

Post a Comment