C Provide variety of storage class specifiers that can be used to declare explicitly the scope and lifetime of variables.
Syntax : storage_class data_type identifier;
where , storage_class is any class[auto,satic,extern and regular]
data_type refers to any existing data
identifier is the variable name.
Types of Class in C Programming are :
1.auto.
2.static.
3.extern.
4.regular.
1. auto : The auto class as the following characteristics
- Storage: memory.
- Default initial value: garbage value.
- Scope: local to the block in which the variable is defined.
- Life: till the control remains within the block in which the variable is defined.
Below is a program to illustrate the use of auto.
#include <stdio.h>
void main()
{
auto int i=1;
{
auto int i=2;
{
auto int i=3;
{
printf("%d", i);
}
printf("%d", i);
}
printf("%d", i);
}
}
Output : 3 2 1
2. register : The register class as the following characteristics
- Storage: CPU Register.
- Default initial value: garbage value.
- Scope: local to the block in which the variable is defined.
- Life: till the control remains within the block in which the variable is defined.
Below is a program to illustrate the use of register.
#include <stdio.h>
void main()
{
for(int i=1;i<10;i++)
{
printf(" %d", i);
}
}
Output : 1 2 3 4 5 6 7 8 9
3. static : The static class as the following characteristics
- Storage: memory.
- Default initial value: zero.
- Scope: local to the block in which the variable is defined.
- Life: value of the variable persists between different function calls.
Below are 2 programs to differentiate between auto and static class specifiers
1st Program
#include <stdio.h>
increment()
void main()
{
increment();
increment();
increment();
}
increment();
{
auto int i=1;
printf("%d\t",t);
i++;
}
Output : 1 1 1
2nd Program
#include <stdio.h>
increment()
void main()
{
increment();
increment();
increment();
}
increment();
{
static int i=1;
printf("%d\t",t);
i++;
}
Output : 1 2 3
The difference between two programs 1st auto and 2nd static storage class for variable The scope of auto and static both use local to the block in which the variable is declared.
Those program consists two functions main () and increment ().
The increment () function called from the main () function for three times.
Each time increments the value of “i' and print.
When in auto each time increment() is called it prints i and increments it but when the increment() is called again it re-initializes i to 1 whereas in static when increment() is called the i value is incremented by 1 and when the next increment() is called it prints the incremented value.
4.extern : The extern class as the following characteristics
- Storage : memory
- Default initial value : zero.
- Scope ; Global.
- Life : As long as the program's execution does not come to an end.
- The extern keyword is used before a variable to inform the compiler that this variable is declared somewhere else. The extern declaration does not allocate storage for variables
Below is an example on how to use extern
0 Comments