Storage classes in C
Storage Classes in C
In C, variables are declared based on the type of data they hold.
During program execution, variables may be stored in:
-
CPU registers, or
-
Primary memory
To specify:
-
where variables are stored
-
how long they exist
-
where they can be accessed
-
their default values
C provides four storage class specifiers:
-
Automatic
-
External
-
Register
-
Static
What Storage Classes Define
Storage classes specify:
-
Storage location (memory or CPU register)
-
Default initial value
-
Scope (where accessible)
-
Lifetime (how long stored in memory)
To fully define a variable, we must mention:
-
Data type
-
Storage class
A variable name identifies a physical memory location where bits are stored.
Lifetime of a Variable
Definition
Lifetime = duration between memory allocation and deallocation
Types of Lifetime in C
1. Automatic Lifetime
-
Created when execution enters a block/function
-
Destroyed when execution leaves it
-
Stored in function call stack
2. Static Lifetime
-
Created once when declared first time
-
Exists until program termination
-
Stored in data segment
3. Dynamic Lifetime
-
Memory allocated/deallocated manually
-
Uses:
-
malloc() -
free()
-
-
Stored in heap
Scope of a Variable
Definition
Scope = region where the variable is accessible
(Scope is a subset of lifetime)
Types of Scope
1. Global Scope
-
Declared outside all functions
-
Accessible throughout program
2. Local Scope
-
Declared inside function/block
-
Accessible only within that block
3. Function Scope
-
Variables passed as formal parameters
-
Accessible within that function
Summary Table: Storage Classes
| Storage Class | Storage Area | Default Value | Lifetime | Scope | Keyword |
|---|---|---|---|---|---|
| Automatic | Memory | Garbage | Till block executes | Local | auto |
| Register | CPU Register | Garbage | Till block executes | Local | register |
| Static | Memory | Zero | Between function calls | Local | static |
| External | Memory | Zero | Entire program | Global | extern |
Declaration Syntax
1. Automatic Storage Class
-
Default for variables declared inside functions
-
Keyword:
auto -
Stored in primary memory
-
Acts as local variable
-
Not initialized by default (garbage value)
Example
2. External Storage Class
-
Used for global variables shared across multiple files
-
Keyword:
extern -
Stored in primary memory
-
Default value = 0
-
Exists throughout program execution
Example
File: prgm1.c
File: prgm2.c
Output
3. Register Storage Class
-
Stored in CPU register for faster access
-
Keyword:
register -
Usually applied to int and char
-
Default value = garbage
-
Scope limited to function/block
Example
Output
4. Static Storage Class
Makes a variable permanent within a region
Types
-
Static local variable
-
Static global variable
Default value = 0
Static Local Variable
-
Stored permanently in memory
-
Accessible only within function/block
-
Retains previous value between function calls
Example
Output
University Question
Code
If called 5 times → Output
Justification
-
static int count = 0;initializes only once -
Variable retains its value between function calls
-
Each call increments the same stored variable
Comments
Post a Comment