- Memory location
- Initial value if not initialized
- Life time
This article explains what storage classes are in C language. So, there are 4 different storage classes in C programming language:
- Automatic (auto)
- External (extern)
- Static (static)
- Register (register)
Here, the terms in the brackets represent the keywords utilized for that specific storage class. Below is a basic table that we will delve into with illustrations:
| Storage class keyword | Where is the variable stored | Initial value of the variable | Scope | Lifetime |
|---|---|---|---|---|
auto |
Stack | garbage | Within the code block it is used | End of the code block |
| extern | Data segment | 0 | Global( Active across multiple files) | Till the end of the program |
| static | Data segment | 0 | Within the block of the code | Till the end of the program |
| register | CPU register | garbage | Within the block of code | End of the code block |
The four features of every variable:
- Scope of the variable: The scope of a variable refers to the parts of the program till which the value of the variable can be accessed.
- Lifetime of the variable: This determines how long the variable will stay alive across the whole program.
- Initial value: The value a compiler gives to a variable when we just declare it without giving it any value.
- Storage: Determines where the variable is stored.
Explanation
1. "Automatic" storage class
This serves as the default storage class for all variables, eliminating the need for separate declaration. They are only accessible within the code block where they are defined and can also be reached within any nested blocks.
If there is a need to retrieve a variable outside of its code block, pointers are employed to reference that variable and access it by dereferencing *pointer.
We utilize the keyword " auto " to retrieve an automatic variable. In cases where it is not initialized, the compiler assigns a random value to the variable, allowing us to update it at a later stage within the program.
Quick view:
Storage: Stack in the processor
Lifetime: In that particular block
Scope: Permitted to that particular block
Initial value: garbage value
Syntax:
auto data_type variable_name = value;
Example program:
#include<stdio.h>
int main ()
{
auto int num =100;
{
auto int num = 300;
{
auto int num = 500;
{
printf("\nnum=%d",num);
}
}
printf ("\nnum=%d",num);
}
printf ("\nnum=%d", num);
return 0;
}
Output:
num=500
num=300
num=100
Understanding:
Distinct code sections within a program are delineated by different blocks for enhanced clarity. Automatic variables have a scope limited to the block in which they are declared. Consequently, during execution, the innermost code block displayed the value of 'num' as 500. Subsequently, the outer block showcased 'num' with a value of 300, followed by the outermost block where the value of 'num' was revealed as 100.
2. "External" storage class
Essentially, an extern variable functions as a global variable, allowing access from any part of the program regardless of its original declaration location - whether in a different code block or within a function. The initial value can be altered throughout the program. By simply adding the "extern" keyword before the variable in the declaration, we enable this functionality. In essence, an extern variable's value is set at the beginning of the program and can be utilized throughout the program. The use of the extern keyword expands the scope and lifespan of the variable.
Purpose: The primary reason for utilizing an external variable is for seamless access across multiple files within a large program, ensuring clear readability with minimal confusion.
- There is no requirement to explicitly include the "extern" keyword when declaring an external variable within a function, as it is automatically inferred due to the functions' visibility across the entire program.
Quick view:
Storage: In the data segment (Processor)
Lifetime: Till the end of the program
Scope: Global (Alive in multiple files)
Initial value: Zero (0)
Syntax:
extern data_type variable_name = value;
Example program:
//To demonstrate the declaration of the variable and the initial value
#include<stdio.h>
extern int num;
int main()
{ printf("%d",num);
return 0;
}
Output:
Understanding: An extern variable is typically defined before the main function, allowing it to be accessed globally throughout the program. When declared in this manner, it remains uninitialized and defaults to a value of 0 as assigned by the compiler when printed.
//To demonstrate the global availability
#include <stdio.h>
void display();
extern int num = 5;
int main()
{
++num;
display();
return 0;
}
void display()
{
++num;
printf("num = %d", num);
}
Output:
num = 7
What is the difference between a global variable and an extern variable?
- Simply said, when we use the keyword extern, it doesn't create a variable that is going to be available globally. Instead, we are accessing a global variable that is defined elsewhere in other module.
- By using the extern keyword before a variable, we are telling the compiler that there is this variable defined in another module. Don't allocate separate storage for it, just find it and access it from that module to use in this program.
3. "static" storage class
A static variable is recognized for its ability to retain values. It maintains the stored value even beyond its scope until the program terminates. It holds the most recent updated value within its storage. There is no requirement to repeatedly declare it for utilization; instead, it is re-initialized.
Quick view:
Storage: In the data segment (Processor)
Lifetime: Till the end of the program
Scope: Within the local block
Initial value: Zero (0)
Syntax:
static data_type variable_name = value;
Example program:
#include <stdio.h>
void display();
int main()
{
display();
display();
}
void display()
{
static int num = 1;
num += 5;
printf("%d ",num);
}
Output:
Understanding:
The variable num is defined as a static variable with an initial value of 1 within the function declaration block. This confines its scope to that specific block. Upon the first function call, the value is incremented by 5, resulting in 1+5 = 6, which is then displayed. Subsequently, during the second function call, the value of num is now 6, leading to 6+5 = 11. The static nature of the variable ensures that it retains the last modified value, hence it is not reset to 1 between function calls.
4. "register" storage class
These variables function similarly to automatic variables, with the key distinction lying in their storage location. While automatic variables are stored in the stack, register variables are stored in the microprocessor's register if an unoccupied register is present. This characteristic enables register variables to be considerably quicker to access and manipulate compared to automatic variables.
Points to remember:
- If a free register is not available at that moment in the microprocessor, then they are stored in the normal memory only.
- The variables which are frequently needed in the program are declared as register storage class variables to decrease the running time of the variable.
- We cannot obtain the address of a register variable using pointers.
Quick View:
Storage: register class in the microprocessor
Lifetime: In that particular block
Scope: Permitted to that particular block
Initial value: garbage value
Syntax:
register data_type variable_name = value;
Example program:
#include <stdio.h>
int main()
{
register int num = 0;
printf("%d",&num);
}
Output:
Compiler Error
Understanding:
The location of this variable will not be stored in the stack of the processor. Instead, it will be stored within the register class. As a result, the compiler will not have access to its memory address, causing an error to be triggered.