Storage Classes
Introduction
The storage class of a variable in C determines the
life time of the variable if this is 'global' or 'local'.
Along with the life time of a variable, storage
class also determines variable's storage location
(memory or registers), the scope (visibility level)
of the variable, and the initial value of the
variable.
In simpler words, In C language, each variable has
a storage class which decides scope, visibility
and lifetime of that variable. The following storage
classes are most often used in C programming:
 Automatic variables
 Extern variables
 Static variables
 Register variables
Automatic variable
A variable declared inside a function without any
storage class specification, is by default an
automatic variable. They are created when a
function is called and are destroyed automatically
when the function exits. Automatic variables can
also be called local variables because they are
local to a function.
Example:
void main()
{
auto mum = 20 ;
{
auto num = 60 ;
printf("nNum : %d",num);
}
printf("nNum : %d",num);
}
Output :
Num : 60
Num : 20
External variable
The extern keyword is used before a variable to
inform the compiler that this variable is declared
somewhere else.
Example 1:
Example 2:
main()
{
extern int x; //Tells compiler that it is defined
somewhere else
x = 10;
printf("%d",x);
}
int x; //Global variable x
Static variable
A static variable tells the compiler to persist the
variable until the end of program. Instead of
creating and destroying a variable every time
when it comes into and goes out of scope, static
is initialized only once and remains into existence
till the end of program.
Example:
void test();
main()
{
test();
test();
test();
}
void test()
{
static int a = 0;
//Static variable
a = a+1;
printf("%dt",a);
}
Output :
1 2 3
Register variable
Register variable inform the compiler to store the
variable in register instead of memory. Register
variable has faster access than normal variable.
Frequently used variables are kept in register.
Only few variables can be placed inside register.
Example:
#include <stdio.h>
void main()
{
register int i;
int arr[5];// declaring
array
arr[0] = 10;// Initializing
array
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (i=0;i<5;i++)
{
// Accessing each
variable
printf("value of arr[%d]
is %d n", i, arr[i]);
}
}
Summary

Storage classes

  • 1.
  • 2.
    Introduction The storage classof a variable in C determines the life time of the variable if this is 'global' or 'local'. Along with the life time of a variable, storage class also determines variable's storage location (memory or registers), the scope (visibility level) of the variable, and the initial value of the variable.
  • 3.
    In simpler words,In C language, each variable has a storage class which decides scope, visibility and lifetime of that variable. The following storage classes are most often used in C programming:  Automatic variables  Extern variables  Static variables  Register variables
  • 4.
    Automatic variable A variabledeclared inside a function without any storage class specification, is by default an automatic variable. They are created when a function is called and are destroyed automatically when the function exits. Automatic variables can also be called local variables because they are local to a function.
  • 5.
    Example: void main() { auto mum= 20 ; { auto num = 60 ; printf("nNum : %d",num); } printf("nNum : %d",num); } Output : Num : 60 Num : 20
  • 6.
    External variable The externkeyword is used before a variable to inform the compiler that this variable is declared somewhere else.
  • 7.
  • 8.
    Example 2: main() { extern intx; //Tells compiler that it is defined somewhere else x = 10; printf("%d",x); } int x; //Global variable x
  • 9.
    Static variable A staticvariable tells the compiler to persist the variable until the end of program. Instead of creating and destroying a variable every time when it comes into and goes out of scope, static is initialized only once and remains into existence till the end of program.
  • 10.
    Example: void test(); main() { test(); test(); test(); } void test() { staticint a = 0; //Static variable a = a+1; printf("%dt",a); } Output : 1 2 3
  • 11.
    Register variable Register variableinform the compiler to store the variable in register instead of memory. Register variable has faster access than normal variable. Frequently used variables are kept in register. Only few variables can be placed inside register.
  • 12.
    Example: #include <stdio.h> void main() { registerint i; int arr[5];// declaring array arr[0] = 10;// Initializing array arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; for (i=0;i<5;i++) { // Accessing each variable printf("value of arr[%d] is %d n", i, arr[i]); } }
  • 13.