Every Variable in a program has a memory associated with it. Memory Requirement of Variables is different for different types of variables. In C, Memory is allocated & released at different places(storage).

Storage class of variable Determines following things

Where the variable is stored
Scope of Variable
Default initial value
Lifetime of variable


A. Where the variable is a store

Storage Class determines the location of variable, where it is declared. Variables declared with auto storage classes are declared inside the main memory whereas variables declared with keyword register are stored inside the CPU Register.

B. Scope of Variable
Scope of Variable tells compile about the visibility of Variable in the block. Variable may have Block Scope, Local Scope, and External Scope. A scope is a context within a computer program in which a variable name or other identifier is valid and can be used, or within which a declaration has an effect.

C. DefaultInitial Value of the Variable

Whenever we declare a variable in C, a garbage value is assigned to the variable. Garbage Value may be considered as the initial value of the variable. C Programming has different storage classes which have different initial values such as Global Variable have Initial Value as 0 while the Local auto variable have to default initial garbage value

D. Lifetime of variable

A lifetime of the = Time Of variable Declaration – Time of Variable Destruction Suppose we have declared variable inside the main function then variable will be destroyed only when the control comes out of the main .i.e end of the program.

Different Storage Classes:
Auto Storage Class
Static Storage Class
Extern Storage Class
Register Storage Class

Automatic (Auto) storage class
is default storage class variables declared are of type Auto by default In order to Explicit declaration of variable use ‗auto‘ keyword
auto int num1 ; // Explicit Declaration

Features:
Storage Memory
Scope Local / Block Scope

Lifetime Exists as long as Control remains in the
block
Default initial Garbage
Value


void main()
{
auto mum = 20 ;
{
auto num = 60 ;
printf("nNum : %d",num);
}
printf("nNum : %d",num);
}

Note :
Two variables are declared in different blocks, so they are treated as different variables External ( extern ) storage class in C Programming


Variables of this storage class are ―Global variables‖ Global Variables are declared outside the function and are accessible to all functions in the
program Generally, External variables are declared again in the function using keyword extern In order to Explicit declaration of variable use the ‗extern‘ keyword extern int num1; // Explicit Declaration

Storage Memory
Scope Global / File Scope
Lifetime Exists as long as the variable is running
Retains value within the function

int num = 75 ;
void display();
void main()
{
extern int num ;
printf(“nNum : %d”,num);
display();
}
void display()
{
extern int num ;
printf(“nNum : %d”,num);
}

Note :
Declaration within the function indicates that the function uses an external variable Functions belonging to the same source code, does not require declaration (no need to write extern) If a variable is defined outside the source code, then declaration using an extern keyword is required

Static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values
between function calls. The static modifier may also be applied to global variables. When this is done, it causes the
variable’s scope to be restricted to the file in which it is declared. In C programming, when static is used on a class data member, it causes only one copy of that member to be shared by all the objects of its class.

Register Storage Class

Register keyword is used to define a local variable. Local variables are stored in a register instead of RAMAs variable is stored in the register, the Maximum size of variable = Maximum Size of Register unary operator [&] is not associated with it because Value is not stored in RAM instead it is stored in Register

Why do we need Register Variable?
Whenever we declare any variable inside C Program then memory will be randomly allocated at a particular memory location.
We have to keep track of that memory location. We need to access the value at that memory location using ampersand operator/Address Operator i.e (&). If we store the same variable in the register memory then we can access that memory location directly without using the Address operator.
Register variable will be accessed faster than the normal variable thus increasing the operation and program execution. Generally, we use the register variable as Counter.

Also Read: Quick Sort

Write a comment