Storage Classes in C
In C programming, storage classes define the scope (visibility) and lifetime of variables and functions. They also determine the memory location of variables. There are four types of storage classes in C: auto, register, static, and extern.
Types of Storage Classes in C
- auto: Default storage class for local variables.
- register: Used to store variables in the CPU register instead of RAM.
- static: Retains the value of a variable between function calls.
- extern: Used to access global variables declared outside the current file.
Storage Classes in C
Storage Class | Storage Place | Default Value | Scope | Lifetime |
---|---|---|---|---|
auto | RAM | Garbage Value | Local | Within function |
extern | RAM | Zero | Global | Till the end of the main program |
static | RAM | Zero | Local | Till the end of the main program, Retains value between multiple function calls |
register | Register | Garbage Value | Local | Within the function |
1) Auto Storage Class
The auto storage class is the default for all local variables. These variables are automatically created when a function is called and destroyed when the function finishes execution. The auto keyword is optional because it is the default.
- Automatic variables are allocated memory automatically at runtime.
- The visibility of the automatic variables is limited to the block in which they are defined.The scope of the automatic variables is limited to the block in which they are defined.
- The automatic variables are initialized to garbage by default.
- The memory assigned to automatic variables gets freed upon exiting from the block.
- The keyword used for defining automatic variables is auto.
- Every local variable is automatic in C by default.
Example
#include <stdio.h>
void demo()
{
auto int x = 10; // auto is optional
printf("Value of x: %d\n", x);
}
int main()
{
demo();
return 0;
}
Output:
2) Register Storage Class
The register storage class is used to store variables in the CPU registers instead of RAM to provide faster access. It is mainly used for frequently accessed variables in performance-critical applications. However, it's not guaranteed that the variable will be stored in a register, as it depends on the system and compiler.
- The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU.
- We can not dereference the register variables, i.e., we can not use &operator for the register variable.
- The access time of the register variables is faster than the automatic variables.
- The initial default value of the register local variables is 0.
- The register keyword is used for the variable which should be stored in the CPU register. However, it is compiler?s choice whether or not; the variables can be stored in the register.
- We can store pointers into the register, i.e., a register can store the address of a variable.
- Static variables can not be stored into the register since we can not use more than one storage specifier for the same variable.
Example
#include <stdio.h>
void demo()
{
register int x = 100; // Store in register
printf("Value of x: %d\n", x);
}
int main()
{
demo();
return 0;
}
Output:
3) Static Storage Class
The static storage class preserves the value of a variable between function calls. A variable declared as static retains its value even after the function execution is completed and can be accessed again in subsequent calls. The static keyword can be used for both local and global variables.
- The variables defined as static specifier can hold their value between the multiple function calls.
- Static local variables are visible only to the function or the block in which they are defined.
- A same static variable can be declared many times but can be assigned at only one time.
- Default initial value of the static integral variable is 0 otherwise null.
- The visibility of the static global variable is limited to the file in which it has declared.
- The keyword used to define static variable is static.
Static Variable Example:
Example
#include <stdio.h>
void demo()
{
static int x = 10; // static variable
printf("Value of x: %d\n", x);
x++;
}
int main()
{
demo();
demo();
demo();
return 0;
}
Output:
Value of x: 11
Value of x: 12
4) Extern Storage Class
The extern storage class is used to declare a variable that is defined outside the current file. It allows access to variables and functions declared in other files. The extern keyword does not allocate memory for the variable; it simply tells the compiler that the variable is declared elsewhere.
- The external storage class is used to tell the compiler that the variable defined as extern is declared with an external linkage elsewhere in the program.
- The variables declared as extern are not allocated any memory. It is only declaration and intended to specify that the variable is declared elsewhere in the program.
- The default initial value of external integral type is 0 otherwise null.
- We can only initialize the extern variable globally, i.e., we can not initialize the external variable within any block or method.
- An external variable can be declared many times but can be initialized at only once.
- If a variable is declared as external then the compiler searches for that variable to be initialized somewhere in the program which may be extern or static. If it is not, then the compiler will show an error.
Extern Variable Example:
Example
// file1.c
#include <stdio.h>
extern int x; // Declare extern variable
void demo()
{
printf("Value of x: %d\n", x);
}
file2.c
#include <stdio.h>
int x = 100; // Define extern variable
int main()
{
demo();
return 0;
}