A storage class in C specifies important properties of a variable such as its scope, lifetime, and how it is stored and accessed by the program.
A storage class in C specifies important properties of a variable such as its scope, lifetime, and how it is stored and accessed by the program.
A variable declared inside a function is normally used only within that function, while a global variable can be accessed from multiple functions.
C provides four commonly studied storage classes: auto, register, static, and extern.
The auto storage class is the default storage class for local variables declared inside a function or block. The variable exists only while that function or block is executing.
Here, age is a local variable with automatic storage
duration. The keyword auto is normally omitted because
local variables are automatic by default.
The register storage class requests that a variable be kept in a CPU register for faster access when possible. It is generally used for frequently accessed local variables.
Here, count is declared as a register variable.
The compiler decides whether it can actually place the variable
in a CPU register.
The static storage class allows a local variable to retain its value between function calls. A static local variable is initialized only once and continues to exist for the entire execution of the program.
Each time the function is called, count keeps its
previous value instead of starting again from zero.
The extern storage class is used to declare a variable that is defined elsewhere, usually as a global variable. It allows different parts of a program to refer to the same variable.
Here, marks is declared as an external variable.
Its actual definition is provided elsewhere in the program.
| Storage Class | Main Idea |
|---|---|
| auto | Default local variable. |
| register | Requests faster access through a CPU register. |
| static | Retains value between function calls. |
| extern | Refers to a variable defined elsewhere. |
Scope tells where a variable can be accessed, while lifetime tells how long the variable exists during program execution.
A local auto variable is available only inside its
block, while a static local variable continues to
retain its value between calls to the function.
Write a C program to demonstrate the use of a static variable by calling the same function multiple times and observing that the variable retains its previous value.
Declare a local variable using static, increase its
value on every function call, and call the function three times.
A static local variable is initialized only once and retains its value between function calls. Its scope remains local to the function, but its lifetime extends throughout the execution of the program.
Count = 1
Count = 2
Count = 3
If count were an ordinary local variable instead of
static, it would be initialized again whenever the
function is called. The static variable retains its previous value.
| Storage Class | Remember |
|---|---|
| auto | Default for local variables. |
| register | Requests fast access through a CPU register. |
| static | Retains value between function calls. |
| extern | Refers to a variable defined elsewhere. |
Watch a beginner-friendly explanation of storage classes in C programming.
A short handwritten-style revision sheet for storage classes will be provided here.
Use the mind map for quick revision of auto, register, static and extern.