0% found this document useful (0 votes)
70 views

Storage Classes

There are four storage classes in C that determine where a variable's value is stored: automatic, register, static, and external. Automatic variables are stored in memory and their lifetime is limited to the block they are defined in. Register variables are stored in CPU registers for faster access but resources are limited. Static variables are also stored in memory but their value persists between function calls within the same program. External variables are defined in one file and accessed across other files in a program.

Uploaded by

devanathan
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)
70 views

Storage Classes

There are four storage classes in C that determine where a variable's value is stored: automatic, register, static, and external. Automatic variables are stored in memory and their lifetime is limited to the block they are defined in. Register variables are stored in CPU registers for faster access but resources are limited. Static variables are also stored in memory but their value persists between function calls within the same program. External variables are defined in one file and accessed across other files in a program.

Uploaded by

devanathan
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/ 7

www.sakshieducation.

com

STORAGE CLASSES

INTRODUCTION

m
From C compiler’s point of view, a variable name identifies

co
some physical location within the computer memory,
where the string of bits representing the variable’s value is

n.
stored. There are basically two kinds of locations in a

io
computer where such a value may be kept— Memory and

at
CPU registers. It is the variable’s storage class that
determines in which of these two locations the value is stored.
uc
The use of the storage classes is that,
ed

• We can have the idea of, the scope of the variable.


hi

• The initial value of the variable, if the initial value is not assigned.
ks

• Where the variable is stored.


• Life time of the variable.
a
.s

There are four storage classes in C:


w

1. Automatic storage class


w

2. Register storage class


w

3. Static storage class


4. External storage class

Let us examine these storage classes one by one.


 
www.sakshieducation.com
www.sakshieducation.com

TOPIC 1 AUTOMATIC STORAGE CLASS

The variable declared under this storage class has the features like,

9 Storage is in memory.

m
9 Default initial value is garbage value, if initial value is not assigned.

co
9 The variable life time is, till the control remains within the block in which the
variable is defined.

n.
main ( )

io
{

at
auto int i, j ;
printf ( "\n%d %d", i, j ) ;
uc
}
ed

The output of the above program could be...


hi

1211 221
ks

It’s better to understand the life and scope of a variable clearly from the following
example program.
a
.s

main ( )
{
w

auto int i = 1;
{
w

{
w

{
printf ( "\n%d ", i ) ;
i++;
}
printf ( "%d ", i ) ;
}

 
www.sakshieducation.com
www.sakshieducation.com

printf ( "%d", i ) ; i++;

}
}

The output of the above program is: 1 1 1

m
This is because, all printf ( ) statements occur within the outermost block (a block is
all statements enclosed within a pair of braces) in which i has been defined. It

co
means the scope of i is local to the block in which it is defined. The moment the

n.
control comes out of the block in which the variable store defined, the variable and

io
its value is irretrievably lost.

at
TOPIC 2 REGISTER STORAGE CLASS
uc
The variable defined under this storage class has the features like,
ed

9 Storage---------------------------- CPU registers.


hi

9 Default initial value ------------------ garbage value.


ks

9 Scope ----------------------------------- local to the block in which it is defined.


9 Life ------------------------------------------------ till the control remains within the block of
a

the variable which it is defined.


.s
w

If a value stored in the CPU can be accessed quickly compared to the value stored in
memory. At times the variable can be used in the program number of times so in
w

this case for accessing the variable its better to declare the variable under the register
w

storage class.

Note:-

 
www.sakshieducation.com
www.sakshieducation.com

Use register storage class for only those variables that are being used very often in a
program. Reason is, there are very few CPU registers at our disposal and many of
them might be busy doing something else. Make careful utilization of the scarce
resources. A typical application of register storage class is loop counters, which get
used a number of times in a program.

m
Example:-

co
main( )

n.
{
register int i ;

io
for ( i = 1 ; i <= 10 ; i++ )

at
printf ( "\n%d", i ) ;
uc
}

• This register storage class is used for the faster access of variables.
ed

• Common use in counter.


hi
ks

• Register type declaration is not applicable for arrays,pointers and structures.


a

TOPIC 3 STATIC STORAGE CLASS


.s

The features of variable defined to have a static storage class are,


w

9 Storage ----------------------------------memory.
w

9 Default initial value---------------zero.


w

9 Scope --------------------------------local to the block in which it is defined.


9 Life --------------------------------------value of the variable persists between two
function calls.

 
www.sakshieducation.com
www.sakshieducation.com

Like auto variables, static variables are also local to the block in which they are
declared. The difference between them is that static variables don’t disappear when
the function is no longer active. Their values persist. If the control comes back to the
same function again the static variables have the same values they had last time
around.

m
Program -1 :- Program – 2:-

co
main() main()
{ {

n.
Increment (); Increment ();

io
Increment (); Increment ();

at
} }
Increment (); Increment ();
uc
{ {
ed

auto int I =1; static int I =1;


printf (“%d”,I); printf (“%d”,i);
hi

I = I + 1; I = I + 1;
ks

} }
Output :- 1 1 Output :- 1 2
a
.s

In the above example, when variable I is auto, each time increment ( ) is called it is
w

re-initialized to one. When the function terminates, I vanishes and its new value of 2
w

is lost. The result: no matter how many times we call increment ( ), I is initialized to
w

1 every time.

On the other hand, if I is static, it is initialized to 1 only once. It is never initialized


again. During the first call to increment ( ), I is incremented to 2. Because I is static,

 
www.sakshieducation.com
www.sakshieducation.com

this value persists. The next time increment ( ) is called, I is not re-initialized to 1; on
the contrary its old value 2 is still available. This current value of I (i.e. 2) gets
printed and then I = I + 1 adds 1 to I to get a value of 3. When increment ( ) is
called the third time, the current value of i (i.e. 3) gets printed and once again i is
incremented. In short, if the storage class is static then the statement static int I = 1 is

m
executed only once, irrespective of how many times the same function is called.

co
Note:-Use static storage class only if you want the value of a variable to persist
between different function calls.

n.
TOPIC 4 EXTERNAL STORAGE CLASS

io
9 Storage: - memory.

at
9 Life: - as long as the program execution comes to an end.
uc
9 Default value: - zero.
9 Scope: - global.
ed

External variables are declared outside all functions, yet are available to all
hi

functions that care to use them.


Example:-
ks

int i ;
a
.s

main( )
{
w

printf ( "\ni = %d", i ) ;


increment( ) ;
w

increment( ) ;
w

decrement( ) ;
decrement( ) ;
}
increment( )
{
i=i+1;

 
www.sakshieducation.com
www.sakshieducation.com

printf ( "\non incrementing i = %d", i ) ;


}
decrement( )
{
i=i-1;
printf ( "\non decrementing i = %d", i ) ;
}
The output would be:

m
i=0

co
On incrementing i = 1
On incrementing i = 2

n.
On decrementing i = 1
On decrementing i = 0

io
The value of i is available to the functions increment ( ) and decrement ( ) since i has

at
been declared outside all functions.
uc
Note: - The local variable that gets preference over the global variable.
ed

Note:- Use extern storage class for only those variables that are being used by
almost all the functions in the program. This would avoid unnecessary passing of
hi

these variables as arguments when making a function call. Declaring all the variables
ks

as extern would amount to a lot of wastage of memory space because these variables
a

would remain active throughout the life of the program.


.s
w
w
w

 
www.sakshieducation.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy