0% found this document useful (0 votes)
16 views11 pages

Storage Class 2

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

Storage Class 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Storage class in c

UNIT 2

B Y- P R O F R A N I
SINGH
Storage Classes in C

 To fully define a variable, one needs to mention not only its


‘type’ but also its ‘storage class’.
 In other words,not only do all variables have a data type, they
also have a ‘storage class’.
 If we don’t specify the storage class of a variable in its
declaration, the compiler will assume a storage class
depending on the context in which the variable is used.
 There are basically two kinds of locations in a computer
where such a value may be kept— Memory and CPU
registers.
 It is the variable’s storage class that determines in which of
these two types of locations, the value is stored.
Variable’s storage class

A variable’s storage class tells us:


(a) Where the variable would be stored.
(b) What will be the initial value of the variable, if initial value is
not specifically assigned.(i.e. the default initial value).
(c) What is the scope of the variable; i.e. in which functions the
value of the variable would be available.
(d) What is the life of the variable; i.e. how long would the
variable exist.

There are four storage classes in C:


(a) Automatic storage class
(b) Register storage class
(c) Static storage class
(d) External storage class
Automatic Storage Class

 Storage: Memory.
 Default value: An unpredictable value, often called a
garbage value.
 Scope: Local to the block in which the variable is defined.
 Life: Till the control remains within the block in which the
variable is defined.
 The keyword for this storage class is auto
Scope and life of an automatic
variable
Register Storage Class

 Storage: CPU registers.


 Default value: Garbage value.
 Scope: Local to the block in which the variable is defined.
 Life: Till the control remains within the block in which the
variable is defined.
 A value stored in a CPU register can always be accessed
faster than the one that is stored in memory. Therefore, if a
variable is used at many places in a program, it is better to
declare its storage class as register. A good example of
frequently used variables is loop counters.
Tips and Traps

 Here, even though we have declared the storage class of i as


register, we cannot say for sure that the value of i would be
stored in a CPU register.
 Why? Because the number of CPU registers are limited, and
they may be busy doing some other task. What happens in such
an event... the variable works as if its storage class is auto
 Not every type of variable can be stored in a CPU register. For
example, if the microprocessor has 16-bit registers then they
cannot hold a float value or a double value, which require 4
and 8 bytes respectively.
 However, if you use the register storage class for a float or a
double variable, you won’t get any error messages. All that
would happen is the compiler would treat the variables to be of
auto storage class.
Static Storage Class
 Storage: Memory.
 Default value: Zero.
 Scope: Local to the block in which the variable is defined.
 Life: Value of the variable persists between different function
calls.
Static Storage Class

 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.
 All this having been said, a word of advice—avoid using
static variables unless you really need them. Because their
values are kept in memory when the variables are not active,
which means they take up space in memory that could
otherwise be used by other variables.
External Storage Class
 Storage: Memory.
 Default value: Zero.
 Scope: Global.
 Life: As long as the program’s execution doesn’t come to an
end.
External variables are declared outside all functions, yet are
available to all functions that care to use them
Which to Use When

 Use static storage class only if you want the value of a


variable to persist between different function calls.
 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.
 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 these variables as
arguments when making a function call. Declaring all the
variables as extern would amount to a lot of wastage of
memory space because these variables would remain active
throughout the life of the program.
 If you don’t have any of the express needs mentioned above,
then use the auto storage class. In fact, most of the times, we
end up using the auto variables.

You might also like