Unit 2 Storageclassses
Unit 2 Storageclassses
1
Storage Classes
2
Storage classes
• C Storage Classes are used to describe the features of a
variable/function.
• These features basically include the scope, visibility, lifetime, memory
location and initial value of a variable.
• which help us to trace the existence of a particular variable during the
runtime of a program.
• Way to characterize variable:
1) Data type→ type of information represented by a variable like int,
float, char.
2) Storage classes→ permanence of a variable and its scope within the
program.
3
Storage Classes
• From C compiler’s point of view, a variable name identifies some
physical location within the computer
• where the string of bits representing the variable’s value is stored.
• 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
locations the value is stored.
4
Storage Classes tells us:
• Where the variable would be stored.
• What will be the initial value of the variable, if initial value is not
specifically assigned (i.e. the default initial value).
• What is the scope of the variable; i.e. in which functions the value of
the variable would be available.
• What is the life of the variable; i.e. how long would the variable exists.
5
Types of storage classes
• Automatic – Auto
• External – Extern
• Static – static
• Register - register
6
Automatic Storage Class
• The features of a variable defined to have an automatic storage class
are as under:
• The output could be any garbage values (you may get different values,
since garbage values are unpredictable)
• We need to initialize the variable properly
8
Automatic Storage Class
main( )
{
auto int i = 1 ;
{
{
{
printf ( "\n%d ", i ) ;
}
printf ( "%d ", i ) ;
}
printf ( "%d", i ) ;
}
} 9
Automatic Storage Class
• The output of the above program is: 1 1 1
• This is because, all printf( ) statements occur within the outermost block
in which i has been defined.
• a block is all statements enclosed within a pair of braces
• It means the scope of i is local to the block in which it is defined.
• The moment the control comes out of the block in which the variable is
defined,
• the variable and its value is irretrievably lost.
10
Automatic Storage Class
main( )
{
auto int i = 1 ;
{
auto int i = 2 ;
{
auto int i = 3 ;
printf ( "\n%d ", i ) ;
}
printf ( "%d ", i ) ;
}
printf ( "%d", i ) ;
} 11
Automatic Storage Class
• The output of the above program would be: 3 2 1
• Note that the Compiler treats the three i’s as totally different variables,
• since they are defined in different blocks.
• Once the control 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 next innermost block, the
third printf( ) refers to the i with value 1.
12
Automatic Variable-example
#include<stdio.h> long int factorial (auto int n)
long int factorial (int n); {
main() auto int i;
auto long int prod = 1;
{
if(n>1)
auto int n; for (i=2; i<=n; i++)
printf("Enter n:"); prod*=i;
scanf("%d", &n); return (prod);
printf("factorial = %ld", factorial(n)); }
}
13
Register Storage Class
• The features of a variable defined to have an register storage class
are as under:
15
Register Storage Class
• 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
classis auto.
• Not every type of variable can be stored in a CPU register.
16
Static Storage Class
• The features of a variable defined to have an static storage class are
as under:
18
Static storage class
float a,b,c; --> external floating point
void dummy (void);
main ()
{
static float a; --> static floating point,
... --> b and c are external, a is static
...
}
void dummy (void)
{
static int a; --> static
int b; ---> automatic
.... --> c external
}
19
Static storage class
In static,
• Initial value must be expressed as constants not expressions.
• The initial value assigned at the beginning of program
execution, retain throughout the life of the program
• Unless different values are assigned during the course of the
computation.
• Zeros will be assigned to all static variable whose declarations
do not include explicit initial values.
• Hence static variables will always have assigned values.
20
External Storage Class
• The features of a variable defined to have an external storage class
are as under:
22
External Variable
• Also, a normal global variable can be made extern as well by
placing the ‘extern’ keyword before its declaration/definition in
any function/block.
• This basically signifies that we are not initializing a new
variable but instead, we are using/accessing the global variable
only.
• The main purpose of using extern variables is that they can be
accessed between two different files which are part of a large
program.
23
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.
• 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 thelife of the program.
• If you don’t have any of the express needs mentioned above, then use the
auto storage class.
24
Multifile programs
• A file is a collection of information stored on a separate entity within the
computer or on an auxiliary storage device.
• A file can be a collection of data, a source code, a portion of a source program,
or a object program etc.,
• Individual executable files form a single object program.
1) Function
• Within a multifile program, a function definition may be either external or
static.
• External→ recognized throughout the entire program
• Static→ recognized only within the file.
• If storage class is not appear → external
2) variables:
• External variables can be defined in one file and accessed in another. 25