Ppsq14(Explain Storage Classes in c )
Ppsq14(Explain Storage Classes in c )
NAGARANI)
Storage classes in C
In C language, each variable has a storage class which decides the following things:
Scope i.e where the value of the variable would be available inside a program.
Default initial value i.e if we do not explicitly initialize that variable, what will be
its default initial value.
Lifetime of that variable i.e for how long will that variable exist.
1. Automatic variables
2. External variables
3. Static variables
4. Register variables
**************************************************************************************
Automatic variables
Scope: Variable defined with auto storage class are local to the function block inside which
they are defined.
1
STORAGE CLASSES PPS –UNIT-1(M.NAGARANI)
#include<stdio.h>
void main()
int detail;
// or
**************************************************************************************
Scope: Global i.e everywhere in the program. These variables are not bound by any
function, they are available everywhere.
#include<stdio.h>
2
STORAGE CLASSES PPS –UNIT-1(M.NAGARANI)
void main()
number = 10;
/* This is function 1 */
fun1()
number = 20;
/* This is function 1 */
fun2()
3
STORAGE CLASSES PPS –UNIT-1(M.NAGARANI)
Note: Declaring the storage class as global or external for all the variables in a program can
waste a lot of memory space because these variables have a lifetime till the end of the
program. Thus, variables, which are not needed till the end of the program, will still occupy
the memory and thus, memory will be wasted.
extern keyword
The extern keyword is used with a variable to inform the compiler that this variable is
declared somewhere else. The extern declaration does not allocate storage for variables.
int main()
printf("%d", a);
int main()
x = 10;
printf("%d", x);
4
STORAGE CLASSES PPS –UNIT-1(M.NAGARANI)
*******************************************************************************************
Static variables
Explanation:
1. A static variable tells the compiler to persist/save the variable until the end of
program.
2. Instead of creating and destroying a variable every time when it comes into and
goes out of scope, static variable is initialized only once and remains into existence
till the end of the program.
3. A static variable can either be internal or external depending upon the place of
declaration.
4. Scope of internal static variable remains inside the function in which it is defined.
5. External static variables remain restricted to scope of file in which they are
declared.
6. They are assigned 0 (zero) as default value by the compiler.
#include<stdio.h>
int main()
test();
test();
test();
5
STORAGE CLASSES PPS –UNIT-1(M.NAGARANI)
void test()
a = a + 1;
printf("%d\t",a);
123
******************************************************************************************
Register variable
Explanation:
1. Register variables inform the compiler to store the variable in CPU register instead
of memory.
2. Register variables have faster accessibility than a normal variable. Generally, the
frequently used variables are kept in registers.
3. But only a few variables can be placed inside registers.
4. One application of register storage class can be in using loops, where the variable
gets used a number of times in the program, in a very short span of time.
Note: Even though we have declared the storage class of our variable number as register,
we cannot surely say that the value of the variable would be stored in a register. This is
6
STORAGE CLASSES PPS –UNIT-1(M.NAGARANI)
because the number of registers in a CPU are limited. Also, CPU registers are meant to do a
lot of important work. Thus, sometimes they may not be free. In such scenario, the variable
works as if its storage class is auto.
Important note:
We should use static storage class only when we want the value of the variable to
remain same every time we call it using different function calls.
We should use register storage class only for those variables that are used in our
program very often. CPU registers are limited and thus should be used carefully.
We should use external or global storage class only for those variables that are
being used by almost all the functions in the program.
If we do not have the purpose of any of the above mentioned storage classes, then
we should use the automatic storage class.