/    /  Storage Classes

Storage Classes

 

Commonly we are declaring variables with their data types. To fully define a variable it needs to mention its storage class along with its data type. Variables have a data type and storage class.

A variable in C can be one of the four storage classes:

  1. Automatic storage class
  2. Register storage class
  3. Static storage class
  4. External storage class

The general format of a variable statement that uses the storage class is:

 

Storage Classes

 

1. Automatic Storage Class:

 

Features of automatic storage class are as follows:

Storage: Memory

Default Initial Value: Garbage value

Scope: Local to the block where the variable has been defined.

Life: Till the control remains within the block.

  • The keyword for the automatic storage class is auto.
  • Automated variables are declared within a function for use.
  • Memory locations are allocated to these variables when a function is called.
  • When the control leaves the function the memory locations allocated to these variables are released. Their contents are lost and not available to any other functions.
  • If a function is called again new memory locations are allocated again to these variables. The old values are lost. So, automatic variables are also known as local variables.
  • Automatic storage class is the default one to variables.

 

Example:

 

#include<stdio.h>
main()
{
void function1();
void function2();
int m=1000;
function2();
printf("%d\n",m);
}
void function1()
{
int m=10;
printf("%d\n",m);
}
void function2()
{
int m=100;
function1();
printf("%d\n",m);
}

 

Output:

 

Storage Classes

 

 2. Register Storage Class:

 

Features of register storage class are as follows:

Storage: CPU Registers

Default Initial Value: Garbage value

Scope: Local to the block where the variable has been defined.

Life: Till the control remains within the block.

  • The keyword for register storage class is register.
  • A value stored in the CPU register can always be accessed faster than the one which is stored inmemory. Therefore, if a variable is used in many places in a program, it is preferable to declare its storage class as a record.
  • Loopback counters are good examples of commonly used variables.
  • Even though we have declared the storage class as register, it is not sure that the value would be stored in a CPU register. Because the number of CPU registers is limited. At that time storage class is auto.
  • Register storage class is not suitable for float, double and long. Because the CPU registers are usually 16-bit registers. It can therefore hold the int, char and, pointer data types.
  • If we use float as register, the compiler won’t give any error. The Compiler will treat that one as auto.

 

Example:

 

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

 

Output:

 

Storage Classes

 

 3. Static Storage Class:

 

Features of static storage class are as follows:

Storage: Memory

Default Initial Value: Zero

Scope: Local to the block where the variable has been defined.

Life: Value changes between different function calls.

  • The keyword for the static storage class is static.
  • The auto and static variables are local to the block in which they are defined.
  • But a static variable does not disappear when the function is no longer active. Their values persist.
  • This storage is suitable for recursion.
  • Avoid using static variables, because their values are kept in memory when the variables are not inactive. So, this is a wastage of memory.

 

Example:

 

#include <stdio.h>
main()
{
void stat();
int i;
for(i=1;i<=3;i++)
 stat();
}
void stat()
{
static int x=0;
x=x+1;
printf("x=%d\n",x);
}

 

Output:

 

Storage Classes

 

4. External Storage Class:

 

Features of extern storage class are as follows:

Storage: Memory

Default Initial Value: Zero

Scope: Global

Life: Till the control remains within the program.

  • External variables are nothing but global variables.
  • Commonly global variables declaration takes place before the main function.
  • But if we declare after all functions, then we can’t access those variables in the functions.
  • In case a local variable and a global variable have the same name, the local variable will have precedence over the global one in the function where it is declared.
  • Use extern storage class for only those variables which are being used by almost all functions in the program.
  • Declaring all the variables as extern would amount to a lot of wastage of memory space because these variables remain active throughout the life of the program.

 

Example:

 

Source code: prg1.c
#includ<stdio.h>
#include”prg2.c”
int y; // by default global variable acts under extern storage class
main()
{
extern int x;
extern int add(int,int);
printf(“x=%d y=%d”,x,y);
printf(“Sum is %d\n”,add(x,y));
}
Source code: prg2.c
int x=10, y=20;
int add(int a,int b)
{
return a+b;
}

 

OUTPUT:

 

x=10 y=20
Sum is 30

 

Steps to execute extern storage program:

 

  1. Compile the external program prg2.c without any errors.
  2. Compile and execute internal program prg1.c, such that prg1.c links prg2.c during runtime of the program.