Different Storage Class Specifiers in C
Introduction
In C programming language, each and every variable has the following two characteristics:
1. data type (int, char, float, etc.)
2. Storage Class
A variable's scope, lifetime, storage location, and default value are determined by its Storage Class. It indicates where in the program we can access a variable. In C language, a storage class is employed to denote the following:
- Variable’s Scope.
- Location of the variable where it is stored.
- Initialized value of a variable.
- Lifetime of a variable.
So, the data regarding a variable is represented by a storage class.
The compiler is informed regarding the storage class of a variable via the storage class specifiers in C programming.
Types of Storage Classes
Storage classes are divided into four categories. They are:
- Automatic Storage Class
- Static Storage Class
- External Storage Class
- Register Storage Class
What are Storage Class Specifiers in C?
Storage class specifiers in C provide information to the compiler on how and
Where to store a variable, what it should be stored as, what its initial value is, and how long it should be stored are all important considerations.
Syntax:
Storage class specifiers follow this syntax:
storage_class_specifier data_type variable _name;
Different Types of Storage Class Specifiers in C
Storage class specifiers come in four different varieties in C. They are:
1. auto
2. static
3. extern
4. register
If a storage class specifier is not declared. In that case, the variables inside the function body are by default auto, the variables outside the body are static, and the variables within a function are extern.
auto
Variables containing the auto-specifier in their declarations are included in the automated storage class. The characteristics of an auto variable are as follows:
Storage Place: It is stored in the CPU memory.
Initial/default value: Garbage value is the Initial/default value.
Scope: Local scope.
Life: Its life is within the function only.
The variables specified inside a function block are local to that function. The variable is eliminated when the function block ends, so it cannot be used outside.
The auto variable scope is limited to the boundaries of the function. The local variable and this are similar. The default value for all local variables is auto.
Example Program for auto in C
Code
#include<stdio.h>
void increase(void);
int main()
{
increase();
increase();
increase();
increase();
return 0;
}
void increase(void)
{
auto int i = 1 ;
printf( "%d ", i ) ;
i++;
}
Output
![Different Storage Class Specifiers in C](https://static.tutorialandexample.com/c/different-storage-class-specifiers-in-c1.png)
static
A variable with the static specifier belongs to the static storage class. The following characteristics apply to this variable:
Storage Place: It is stored in the CPU memory.
Initial/default value: Zero is the Initial/default value.
Scope: Its scope is local.
Life: A
static
variable retains its value between various function calls.
Static variable example program in C
Code
#include<stdio.h>
void increment(void);
int main()
{
increment();
increment();
increment();
increment();
return 0;
}
void increment(void)
{
static int i = 1 ;
printf( "%d ", i ) ;
i++;
}
Output
![Different Storage Class Specifiers in C](https://static.tutorialandexample.com/c/different-storage-class-specifiers-in-c2.png)
extern
The variables with the extern specifier are part of the external storage class. The properties of this variable are as follows:
Storage Place: It is stored in the CPU memory.
Initial/default value: Zero is the Initial/default value.
Scope: Its scope is global.
Life: It will exist until the main program ends. The variable definition may be anywhere in the program.
An extern variable is defined and accessible anywhere in the main source file or others.
Example Program for extern in C
Code
#include<stdio.h>
int m = 810 ;
int main( )
{
extern int s;
printf("The value of a is %d \n",m);
printf("The value of b is %d",s);
return 0;
}
int s = 111;
Output
![Different Storage Class Specifiers in C](https://static.tutorialandexample.com/c/different-storage-class-specifiers-in-c3.png)
register
A variable specified with the register specifier belongs to the register storage class. The register variable has the following properties:
Storage Place: It is stored in the Register memory.
Initial/default value: Garbage value is the Initial/default value.
Scope: Local scope.
Life: Its life is within the function only.
The variables specified inside a function block are local to that function. Like the auto-specifier, they cannot be accessed outside the function. The register variable, however, is kept in register memory instead of CPU memory, allowing for faster access to the variables. Since the register size is very low, only limited variables can be used as registers.
Register example program in C.
Code
#include <stdio.h>
int main()
{
register int i;
int arr[6];// declaring array
arr[0] = 100;// Initializing array
arr[1] = 200;
arr[2] = 300;
arr[3] = 400;
arr[4] = 500;
arr[5] = 600;
for (i=0;i<6;i++)
{
// Accessing each variable
printf("The value of arr[%d] is %d \n", i, arr[i]);
}
return 0;
}
Output
![Different Storage Class Specifiers in C](https://static.tutorialandexample.com/c/different-storage-class-specifiers-in-c4.png)
Conclusion
We use different variables in different storage classes of the C language, depending on the requirements. If any storage class specifier is not defined explicitly, it would be considered an auto-storage class specifier in C.