Storage classes
The storage class of a variable tells the compiler,
- The storage area of the variable.
- The initial value of the variable if not initialized.
- The scope of the variable.
- Life of the variable i.e. how long the variable would be active in the program.
Types of storage classes
There are four types of storage class in c.
- automatic
- external
- static
- register
Note:
[quote]automatic, static, and external storage classes are kept memory type.[/quote]
Automatic storage class
Automatic variables are declared inside a function in which they are to be utilized. They are created when the function is called and destroyed automatically when the function is exited; hence the name automatic variables are private (local) to the function in which they are declared.
int main () { auto int a; ……; ……; return 0; } |
int main () { int a; ……; ……; return 0; } |
A variable declared inside a function without storage class specification is by default an automatic variable.
We may also use the keyword auto to declare an automatic variable.
Note:
- [quote]
- Auto variables are kept memory type.
- Default value for this variable is an unpredictable value, i.e. called as garbage value.
- Scope of the auto variable within the block, which the variable is defined.
- Life of the auto variable till the control remains within the block, in which the variable is defined.
[/quote]
External storage class
A variable that is both alive and active throughout the program is known as an external variable. They are also known as a global variable can be accessed by any function in the program. An external variable is declared outside a function.
Once a variable has been declared as global, any function can use it and change its value. Then, subsequent functions can reference only that new value.
One other aspect of a global variable is that it is available only from the point of declaration to the end of the program.
void fun(); void main () { y=5; …; …; } int y; void fun () { y=y+1; …..; } |
We have a problem in above program. As far as main is concerned, y is not defined. So, the compiler will issue an error message.
void fun(); void main() { extern int y; y=5; …; …; } int y; void fun () { y=y+1; …..; } |
If you want to access a global variable into our main() function by using the keyword extern; i.e. external declaration.
Note:
- [quote]
- Extern variables are kept memory type.
- The default value for this variable is zero.
- Scope of the extern variable is global; i.e. all functions.
- Life of the extern variable is, as long the program’s execution doesn’t come to an end.
[/quote]
Static storage class
As the name suggests, the value of static variables persists until the end of the program. A variable can be declared static using the keyword static.
A static variable may be either an internal type or external type depending on the place of declaration. An internal static variable is those, which are declared inside a function, the scope of internal static variable, extend up to the end of the function in which they are defined. Therefore, internal static variables are similar to auto variables, except that they remain in existence throughout the program.
An external static variable is declared a variable is declared outside of all functions and is available to all the functions in the program. The difference between a static external variable and external variable is that the static external variable is available only within the file where it is defined while the external variable can be accessed by other files.
A static variable is initialized only once when the program is compiled. It is never initialized again.
Note:
- [quote]
- Static variables are kept type.
- The default value for this variable is zero.
- Local to the block in which the variable is defined.
- Life of the static variable is, as long the program’s execution doesn’t come to an end.
[/quote]
Example:
#include <stdio.h>
void state();
int main()
{
int i;
for (i=0 ; i<=3 ; i++ )
state();
return 0;
}
void state()
{
static int x;
x=x+1;
printf("\nx= %d",x);
}
Output:
[quote]
[/quote]
Register storage class
It is created at the nearest memory available to the CPU named as register. A value stored in a CPU register can always be accessed faster than the one that is stored in memory.
Limitations
[quote]
- We can’t create n no of register variables (maximum 10 or 20) depending on CPU.
- We can’t collect the address of register variable.
[/quote]
Note
[quote]
- The default value for this variable is garbage.
- Scope of the register variable within the block, which the variable is defined.
- Life of the register variable until the control remains within the block, in which the variable is defined.
[/quote]
Global variable Vs Local variable
Global Variable |
Local variable |
|
|
Statis Vs automatic variables
Variable |
Storage class |
Keyword |
Storage Location |
Scope |
Extent |
Initial value |
Automatic variables |
Automatic |
Auto |
Memory |
Local to the block or function in which the automatic variable is declared |
The automatic variable will be alive during the execution of block or function in which it is declared. |
Garbage |
Static variables |
Static |
Local static Global static |
Memory Memory |
Local to the block or function in which static variable is declared Accessible from the place of the declaration to the entire program |
It is alive between two function calls. It is alive during the execution of the entire program. |
Zero Zero |