The Scope, Visibility and Lifetime of Variables
Introduction
Storage classes define the scope and visibility of an identifier such as variable or a function. It is also use to determine memory, lifetime, and initial value of a variable.
In C, all the variables not only have the data types but they also have storage class
Types of Storage classes in C
Automatic variables
- The Automatic variables are declared inside a function in which it is to be be used.
- Automatic variables are created when the function is called and destroyed when the function completes its execution or exited, therefore it is known as automatic variables.
- They are also known as local or internal variables.
- The variables declared inside a function without any storage class is, by default, an automatic variables.
- auto keyword is used to declare auto variables
void main()
{
int n;
}
// the above code can also be written as
void main()
{
auto int n;
// it has the same meaning as above example
}
External variables
- External variables are also known as Global variables.
- They are declared outside of any function.
- External variables can be accessed by any function in program.
- External variables are not only used between multiple functions but also are used with multiple files/programs. Means that the global variables declared in one file/program can accessed by another file/program. (if declared using extern keyword)
- extern keyword used to declare external (other files) global variables, though we can skip it, if external variable is to accessed within the same file by all the function.
- Extern variables are useless when used in the same file. They are mainly designed to export global variables from another file.
extern int i = 99;
//above is accessed from multiple files.
or
int i = 99;
// above is restricted to current program.
void fun1()
{
i = 1;
}
void fun2()
{
i = i + 1;
}
void main()
{
printf ("%d",i);
f1();
printf ("%d",i);
f2();
printf ("%d",i);
}
Static variables
- Static variables have the property of retaining their value even after they have been removed from their scope!
- static variables retain the value of their most recent use within their scope.
- They are only initialized once and exist until the program terminates.
- No new memory is allocated because they have not been re-declared.
- Static variables can be internal or external.
- Static variables declared inside a function are called internal static variables, their scope is limited to the function where they are declared.
- An external static variables are declared outside of all the functions and available to all the function in that program.
- A static variables is initialized only once, when the program is compiled, it is never initialized again even if the function (where it is declared) is recalled multiple times.
void fun1()
{
static int i=1;
}
void main()
{
fun1();
fun1();
}
Register variables
- Register variables are declared in of the machine’s registers instead of memory (RAM).
- Since, register access is faster then memory, it is suggestable to declare frequently used variables in register such as loop control variables
void main()
{
register int i;
for (i=1; i<=5; i++)
{
printf ("it is printed faster using register variables\n");
}
}
Summary of Storage classes
Storage class | Storage | Default Initial Value | Scope | Lifetime |
---|---|---|---|---|
Automatic | Stack | garbage | local within block | end of the block |
Register | Stack or CPU register | garbage | local within block | end of the block |
Static | Static memory | 0 | Local or global, within a block or within the file | End of the program |
External | Static memory | 0 | Global | End of the program |