We all know variables have a data type, but they also have a storage class.
We have not mentioned storage classes yet because storage classes have defaults.
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. thus, variables have certain default storage classes.
We know that variables are stored in a memory location.
There are mainly two types of memory locations in a computer – Memory and CPU registers.
It is a variable storage class in which variable values are stored.
Each storage class decides the following things:
- storage place: Where the variable would be stored.
- default initial value: if initial value is not specifically assigned.
- scope of the variable : where the value of a variable is accessible.
- life time of a variable : how long varible is exist.
Types of storage classes in C
There are 4 types of storage classes available in C language. They are,
- Automatic storage class
- Register storage class
- Static storage class
- External storage class
Let’s see each storage classes one by one.
Automatic storage class
auto keyword is used to declare variable is of automatic stroge class.
For example: auto int x=10;
Storage place | Memory |
Default initial value | Garbage value |
Scope | Local |
Life time | within the block in which it is defined. |
Now lets see an example to understand how an automatic storage class variable is declared and what is the initial value if variable is not defined.
Example –
1 2 3 4 5 |
int main() { auto int i, j; printf("%d %d", i, j); } |
Output:
1 |
2074 654 |
The variable is declared of automatic storage class by using auto keyword.
where 2074 and 654 are garbage values of i and j. So it is clear that if you do not define any value to variable then compiler will initialize it garbage value.
The Scope and life of an automatic variable are illustrated in the following program.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int main() { auto int a=1; { auto int a=2; { auto int a=3; printf("%d\n", a); } printf( "%d\n ", a); } printf("%d", a); } |
Output:
1 2 3 |
3 2 1 |
In above program the compiler treats the three a’s as totally different variables.
Because they are defined in different blocks.
Since the lifetime of automatic storage class is within the block where it is defined, the value of a is changed.
Register storage class
register keyword is used to declare variable is of register storage class.
For example: register int x=10;
Storage place | CPU registers |
Default initial value | Garbage value |
Scope | Local |
Life time | within the block in which it is defined. |
A value stored in a CPU register can always be accessed faster than the one that is stored in memory. But the number of CPU registers are limited. Therefore if space is not available in registers then the variable will automatically be treated as a auto variable.
We can not find the address of a register variable because register variables are represented by there names. eg – AX, BX, CX.
Example
1 2 3 4 5 |
int main() { register int a=20; printf("address of register variable is %d", &a); //error(& can not be appiled on register variable) } |
Static storage class
static keyword is used to declare variable is of static storage class.
Mainly, the static variable is used to count how many times a function has been called.
Reinitialisation of static variable is not allowed. Only one time value get initializsed
For example: static int x=10;
Storage place | Memory |
Default initial value | Zero |
Scope | Local |
Life time | throuthout the program |
Example
1 2 3 4 5 6 7 8 9 10 11 12 |
int main() { show(); show(); show(); } void show() { static int i=1; i++; printf("show() is called %d times.\n", i); } |
Output
1 2 3 |
show() is called 2 times. show() is called 3 times. show() is called 4 times. |
The function show( ) gets called from main( ) thrice. Each time it increments the value of i and prints it.
Static variable doesn’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. And it is not reinitialized.
External storage class
extern keyword is used to declare variable is of static storage class.
For example – extern int a=20;
External variables differ from another variable in their scope i.e. global.
As external variables scope is global, they are declared outside all functions.
Storage place | Memory |
Default initial value | Zero |
Scope | Global |
Life time | across the file |
Example
1 2 3 4 5 6 7 8 9 |
#include<stdio.h> int x =10; int main( ) { extern int y ; printf( "The value of x=%d\n", x); printf( "The value of y=%d", y); } int y = 20 ; |
Output
1 2 |
The value of x=10 The value of y=20 |
Here, x and y both are global variables. Both variables are defined outside the all functions.
Which storage class is use when?
Rules for usage of different storage classes in different programming situation.
- By default, if variable is not declared explicitly then it is of auto storage class.
- Use register storage class for only those variables that are being used very often in a program. Because CPU registers are limited. Mainly register storage class is use as loop counters.
- If you want the value of a variable to persist between different function calls, then use static storage class.
- When you have multiple functions in the program that use a common variable, then use extern storage class.
This would avoid the declaring a variable in each function and save the memory space. Because it is active throughout the life of the program.
Difference between the local and static variable
Local variable | Static variable | |||||
---|---|---|---|---|---|---|
Storage class | Local variables are belong to automatic storage class | Static variables are belong to static storage class | ||||
Storage | Stack section | Data section | ||||
Default value | Garbage value | Zero | ||||
Reinitialisation | Local variable can be reinitialisation. | Static variable can not br reinitialise | ||||
Scope | Within the function | throughout the program | ||||
Life time | Lifetime of local variable is within the block. | Lifetime of static variable is throughout the program. | ||||
Example |
|
|
||||
Output | 0 0 0 | 0 1 2 |
In above programs, there are two functions, main() and show().
show() gets called from main() three time. Each time it increments the value of x and prints it.
The difference between them is that when variable x is auto, it is re-initialized to zero.
On the other hand, if x is static, it is initialized to 0 only once. It is never initialized again.
In short, if the storage class is static then the statement static int x = 0 is executed only once, irrespective of how many times the same function is called.