[go: up one dir, main page]

0% found this document useful (0 votes)
68 views8 pages

EST102: Programming in C: Storage Classes

The document discusses different storage classes in C programming including automatic, register, static, and external. It explains the storage location, default initial value, scope, and lifetime of variables for each storage class. Examples are provided to illustrate the concepts.

Uploaded by

Mercy Saji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views8 pages

EST102: Programming in C: Storage Classes

The document discusses different storage classes in C programming including automatic, register, static, and external. It explains the storage location, default initial value, scope, and lifetime of variables for each storage class. Examples are provided to illustrate the concepts.

Uploaded by

Mercy Saji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

EST102 : Programming in C

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:

1. Storage – whether the variable will be stored in memory or in registers.

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.

There are four storage classes:

1. Automatic

2. Register

3. Static

4. Global or external

158
11.1. INTRODUCTION 159

11.1.1 Automatic storage class


The features of a variable defined with automatic storage class are:
Storage − memory
Default initial value − an unpredictable value, which is often called a garbage
value or a junk value.
Scope − local to the block in which variable is defined.
Lifetime − till the control remains within the block in which the
variable is defined.
To assign automatic storage class to a variable, you use the keyword auto. See the
example below:

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. ■

Example 11.2. This example illustrates the scope of an automatic variable.


main()
{
auto int i=1;
{
auto int i=2;
{
auto int i=3;
printf("%d ",i);
}
printf("%d ", i);
}
printf("%d ", i);
}
The output of the above program would be:
3 2 1
The compiler treats the three i’s as totally different variables, since they are defined in
three different blocks (piece of code enclosed between a pair of braces). Once the control
160 CHAPTER 11. STORAGE CLASSES

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. ■

11.1.2 Register storage class

The features of a variable defined with register storage class are:


Storage − CPU registers
Default initial value − garbage value .
Scope − local to the block in which variable is defined.
Lifetime − till the control remains within the block in which the variable
is defined.
Values stored in CPU registers can always be accessed faster than those that are stored
in memory. Therefore, if a variable is used at many places in a program, it is advisable to
declare its storage class as register. A good example of a frequently used variable is a
loop variable.
The number of CPU registers is limited, so you need to carefully choose which variables
are to be assigned register class. Moreover, even if you assign register storage class to
a variable, it is the discretion of compiler to decide where it should be stored. If there are
no free registers available, the variable works as if its storage class is auto.
An important point to note that if a variable is stored in a register, it has no address. If
you try to print its address using & operator, you will end up with an error. For example,
when you compile the code
main()
{
register int a;
printf("%p",&a);
}

the result will be


error: address of register variable `a' requested

11.1.3 Static storage class

The features of a variable defined with static storage class are:


11.1. INTRODUCTION 161

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

11.1.4 External storage class


The features of a variable whose storage class has been defined as external are:
162 CHAPTER 11. 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

Consider the code


main()
{
printf("x is %d",x);
}
int x=21;

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 declaration and the statement


int x=21;

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

local variable will be given preference as illustrated below:


int x = 10;
void display()
{
printf("%d ",x); //prints the global value
}
main()
{
int x = 20 ;
printf("%d ",x); //prints the local value.
display();
}

This code will output


20 10

R If no storage class is specified in the variable declaration, then compiler will


assign it the automatic storage class.

11.2 Programming examples


Program 11.80. To reverse a number using recursion

#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.

You might also like