3.7 Storage classes
Storage classes: -
- A storage class defines scope, lifetime, and visibility of variables or functions.
- It tells the compiler:
i . Where the variable is stored in
memory.
ii. How long it lives (lifetime).
iii. Which parts of the program can access
it (scope).
- C provides four primary storage classes:
1. auto
2. register
3. static
4. extern
1) auto storage class: -
- By default, local variables inside functions have the auto storage class, which means the keyword auto is optional and usually not written.
- Local variables are stored in the stack memory, which is a region of memory used for temporary storage during function execution.
- The scope of a local variable is limited to the block in which it is declared. This means it cannot be accessed outside the function or block.
- The lifetime of a local variable lasts only as long as the function or block is executing. Once the function ends, the memory for the variable is released, and the variable no longer exists.
- auto int a = 10; is same as int a = 10;
Output: - a = 10
2) register storage class: -
- A register variable is a type of local variable that is stored in the CPU registers instead of RAM to allow faster access during program execution.
- The scope of a register variable is limited to the block in which it is declared. This means it cannot be accessed outside the function or block.
- The lifetime of a register variable lasts only for the duration of the block or function in which it is defined. Once the block ends, the variable ceases to exist.
- The keyword register is used to declare such variables. For example:
- We cannot take the address of a register variable using the & operator because it may reside in a CPU register, not in memory.
Output: -
3) Static storage class: -
- A static variable is a variable that retains its value throughout the entire program execution, even after the function in which it is declared finishes.
- The lifetime of a static variable is the entire duration of the program, which is why it preserves its value between multiple calls to the same function.
- If a static variable is declared inside a function, its scope is local to that function, meaning it cannot be accessed from outside the function.
- If declared outside all functions, it has a global scope restricted to the file in which it is declared.
- Static variables are stored in the data segment of memory, not in the stack. This allows their value to persist even after the function execution ends.
- The keyword static is used to declare such variables. For example:
Example: -
Output: -
4) extern storage class: -
- The extern keyword is used to access global variables that are defined in another file. This allows multiple files in a program to share the same variable.
- An extern variable has global scope, which means it can be accessed from any function in any file that declares it with extern.
- The lifetime of an extern variable is the entire duration of the program, just like other global variables.
- The keyword extern is used when we declare a variable that is defined elsewhere. For example:
- extern does not allocate memory for the variable; it only tells the compiler that the variable exists somewhere else.
- The actual variable must be defined in one of the program files without extern.
Example: -
Output: -
|
Storage Class |
Default value |
Scope |
Lifetime |
Memory Location |
Keyword |
Notes |
|
auto |
Garbage |
Local
to block/function |
Exists
only during block execution |
Stack |
auto
(optional) |
Default
for local variables; value lost after function ends |
|
register |
Garbage |
Local
to block/function |
Exists
only during block execution |
CPU
registers (if possible) |
register |
Faster
access; cannot take address using & |
|
static |
0 |
Local
(if inside function) or global (if outside) |
Entire
program run |
Data
segment |
static |
Retains
value between function calls |
|
extern |
0 |
Global,
accessible across files |
Entire
program run |
Data
segment |
extern |
Used
to access global variables defined in another file; does not allocate memory |