You are on page 1of 17

Storage Classes

Behavior of Variables
• Variables in C differ in behavior from those in
most other languages.
• e.g. In BASIC program, a variable retains its
value throughout the program.
• In C variable have data type as well as they
have storage class.
• It is important to understand storage classes and
utility to develop multifunction program.
• Scope:
- determines over what region of the program
variable is actually available for use. (active
region of variable)
• Longevity:
- refers to the period during which a variable
retains a given value during execution of
program (alive)
• Visibility:
- refers to the accessibility of a variable from

the memory.
Storage classes
• Automatic variables
• External variables
• Static variables
• Register variables
• Variables may be broadly categorized based on
there declaration as local(internal) or
global(external) .
Automatic Variables
• They are declared inside a function in which they are to
be used.
• Automatically created when function is called and
destroyed when function is exited.
• So they are called automatic variables.
• They are private to function.
• So they are also called local or internal variables.
• Variable declared inside function without storage class
specification is by default, an automatic variable.
• auto keyword is used to declare automatic variable.
Automatic variables (Contd.)
void fun1(void);
void fun2(void);
main()
{
int m=1000;
fun2();
printf(“%d”,m); output: 10
} 100
void fun1(void) 1000
{
int m=10;
printf(“%d”,m);
}
void fun2(void)
{
int m=100;
fun1();
printf(“%d”,m)
}
Automatic variables (Contd.)
• Note:
1. Value of automatic variables cannot be changed
accidentally in another program.
2. Any variable local to main will normally alive throughout the
whole program, while it is active only in main.
3. During recursion nested variables are unique auto
variables (with same name).
External Variables
• Variables that are both active and alive
throughout entire program are called external
variables.
• Also known as global variables.
• Can be accessed by any function.
• Subsequent function can refer only new value.
• They are declared outside function.
• In case local and global variable have same
name, the local variable will have higher
precedence over global variable in the function
where it is declared.
External Variables (Contd.)
int fun2()
int fun1(); {
int fun2(); int x=1; /*local*/
int fun3(); return x;
int x /* global */ }
main() int fun3()
{ {
x=10; /*global */ x=x+10; /*global */
printf(“x=%d”,x); }
printf(“x=%d\n”,fun1());
printf(“x=%d\n”,fun2()); Output:
printf(“x=%d\n”,fun3()) x=10
} x=20
int fun1() x=1
{ x=30
x=x+10;
return x;
}
External declaration
• main()
{
y=5;
}
int y;
func1()
{
y=y+1;
}
• Main cannot access the variable y as it is declared after
main function.
• So compiler will generate an error message.
• This problem can be solved by declaring variable with
storage class extern.
External declaration (Contd.)
main()
{
extern int a; /*External declaration*/
a=10;
printf(“%d”,a);
}
Fun1()
{
extern int a; /*External declaration*/
a=11;
printf(“\nValue of a in function %d”,a);
}
int a;
Output: 10
value of a in function 11
External declaration (Contd.)
• Variable a is declared after main and function in
previous program.
• External declaration informs the compiler that a
is of type integer and it is defined somewhere
else in the program.
• extern declaration do not allocate the memory
space for variable.
• We can have external declaration for arrays and
functions.
Static variables
• The value of static variables persists until the
end of program.
• They are declared using static keyword.
e.g. static int a;
• Static variable may be of either internal type or
external type.
• Internal static variable are those declared inside
function.
• The scope of internal static variables is only till
the end of function but they remain alive
throughout the program.
Static variables (Contd.)
void stat()
{
static int x=0; output: 1 2 3
x=x+1;
printf(“%d\t”,x);
}
main()
{
int i;
for(i=0;i<3;i++)
stat();
}
• Static variables are initialized only once when the program is
compiled. It is never initialized again.
• Because x is static it’s value persist between two function calls.
Static variables (Contd.)
• External static variable is declared outside of all
functions and it is available to all functions in that
program.
Register Variables
• Normally all variables are stored in memory.
• But register variables are stored in machine’s
memory.
• The register access if faster compared to
memory access.
• So we can keep the frequently access variables
in register for faster execution of programs.
• They are declared using register keyword.
e.g register int a;
Register Variables (Contd.)
• Only few variables can be kept in register so we
should carefully select them.
• C will automatically convert register variables
into non-register variables once the limit has
been reached.

You might also like