EST102: Programming in C: Storage Classes
EST102: Programming in C: Storage Classes
Chapter 11
Storage classes
Narasimhan T.
11.1 Introduction
To fully define a variable you need to mention not only its data type but also its ‘storage
class’. A variable’s storage class gives the following pieces of information:
2. Default initial value – what will be the initial value of the variable, if initial value is
not specifically assigned.
3. Scope – over what regions of the program, the variable’s value can be accessed. The
scope denotes the active regions of the variable.
4. Lifetime – period of time during which a variable retains its value during the program
execution. Lifetime (also called longevity) denotes the alive period of the variable.
1. Automatic
2. Register
3. Static
4. Global or external
158
11.1. INTRODUCTION 159
Example 11.1. Following code shows how an automatic storage class variable is declared,
and also the fact that if the variable is not initialized, it contains a garbage value.
main ()
{
auto int i,j;
printf("\n %d %d",i ,j);
}
When you run this code, you will get junk values as output. These values will differ each
time you run the code. ■
comes out of the innermost block, the variable i with value 3 is lost, and hence the i in
the second printf() refers to i with value 2. Similarly, when the control comes out of the
immediate outer block, the third printf() refers to the i with value 1. ■
Storage − memory
Default initial value − zero.
Scope − local to the block in which variable is defined.
Lifetime − value of the variable persists till the end of the program.
Like auto variables, static variables are also local to the block in which they are declared.
However, static variables don’t disappear when you get out of that block. Their values
persist. If the control comes back to the same block again, the static variables will have the
same values they had last time around.
Example 11.3. Figure 11.1 lists two similar codes. Code 1 declares a variable i with auto
storage class. The same variable is declared with static storage class in Code 2.
When variable i is auto, each time increment() is called, it is re-initialized to one. When
the function terminates, i vanishes and its updated value is lost. The result is that, no
matter how many times you call increment(), i is initialized to 1 every time. Thus the
output produced by Code 1 is
1 1 1
On the other hand if i is static, it won’t be reinitialized every time the function is called.
After the first call of increment(), i becomes 2 and next time, when control reaches
increment(), the value 2 will be retained and this value is incremented. Thus Code 2
outputs 1 2 3 ■
Code 1 Code 2
void increment() void increment()
{ {
auto int i=1; static int i=1;
printf("%d ", i); printf("%d ", i);
i=i+1; i=i+1;
} }
main() main()
{ {
increment(); increment();
increment(); increment();
increment(); increment();
} }
Figure 11.1: Illustration of lifetime with auto and static storage classes
Storage − memory
Default initial value − zero.
Scope − global, i.e., available anywhere within the program.
Lifetime − value is accessible as long as the program’s execution
doesn’t come to an end.
The variables that are available to all the functions i.e. those variables which can be
accessed from anywhere within the program are known as external or global variables.
External variables are declared outside all functions.
Example 11.4. This example illustrates the fact that global variables are declared outside
all functions and the fact that they will be assigned the value zero if not specified in the
program. An important point to note is that since global variables are accessible all over
the program, changes made to them by one function will be reflected in other functions too.
See the program below:
void increment()
{
i=i+1;
printf("\n On incrementing i= %d", i);
}
void decrement()
{
i=i-1;
printf("\n On decrementing i= %d", i);
}
int i ;
main()
{
printf("i=%d\n", i );
increment();
increment();
decrement();
decrement();
}
The output of the above program would be:
i=0
On incrementing i=1
On incrementing i=2
On decrementing i=1
On decrementing i=0
Since i is global and is uninitialized in the program, it will be assigned zero. The changes
made in increment() and decrement() are available throughout the program. ■
11.1. INTRODUCTION 163
Here x is global as it is declared outside the function. But as far as main() is considered,
x is undeclared. The printf() statement tries to print the value of a variable which is not
declared yet. Thus when you compile the code, the result will be
error: `x' undeclared
To get around this error, you should inform the compiler that x is not undeclared rather
the declaration occurs after main(). This is achieved by using extern keyword. See the
rectified code below:
main()
{
extern int x;
printf("x is %d",x);
}
int x=21;
This outputs
x is 21
The statement
extern int x;
is called external variable definition. External variable declaration cannot include as-
signment of values but external variable definition can contain assignment as in the case
above. Thus the code
main()
{
extern int x=5;
printf("x is %d",x);
}
int x=21;
yields you an error. A final point to make about external variables is that if a local variable
and a global variable are available within a block and both have the same name, then the
164 CHAPTER 11. STORAGE CLASSES
#include<stdio.h>
int sum,rem;
int reverse(int num)
{
if(num>0)
{
rem=num%10;
sum=sum*10+rem;
reverse(num/10);
}
else
return sum;
}
main()
{
int num,rev;
printf("Enter a number:");
scanf("%d",&num);
11.2. PROGRAMMING EXAMPLES 165
rev=reverse(num);
printf("The reverse of %d is %d",num,rev);
}
Note that since we want the variables rem and sum to retain their values across function
calls, they are declared as global.