5.4Scope, Lifetime and the auto and extern Storage Classes
Covers IGNOU MCS-201 §5.6, 5.6.1, 5.6.2
Once a program has several functions, two questions arise for every variable: where in the program can it be used, and how long does it exist? C answers both through storage classes. This section covers the two most common: automatic (local) and external (global).
Scope and lifetime
A variable’s storage class determines its scope, its lifetime, its default initial value and where it is stored. C has four storage-class keywords for variables:
Storage class
Keyword
Scope
Lifetime
Default initial value
Automatic
auto (optional)
The block where it is declared
Until that block ends
Garbage (indeterminate)
External
extern
From its declaration to the end of the file; all files if declared there
The whole program run
0
Static (local)
static
The block where it is declared
The whole program run
0
Register
register
The block where it is declared
Until that block ends
Garbage (indeterminate)
Automatic (local) variables
Variables declared inside a function or block, including the function’s parameters, are automatic variables. They are created when the block is entered and destroyed when it ends. The keyword auto is the default and is almost never written.
Two functions may use the same local name; they are different variables.
Without an initialiser, a local variable holds garbage.
The value does not survive from one call of the function to the next.
External (global) variables
A variable defined outside all functions is a global variable. Any function that follows it in the file can read and change it, and it exists for the whole run of the program, starting at 0 if not initialised.
Definition
Declaration with extern
Form
int gv = 10; outside all functions
extern int gv;
Allocates memory
Yes
No
May have an initial value
Yes (a constant expression)
No
Needed when
Once per program
The variable is defined later in the file or in another file
If a local variable has the same name as a global one, the local name hides the global one inside that block.
Global variables are convenient but risky: any function can change them, which makes errors hard to trace. Prefer passing values as arguments and returning results.
Key takeaways
A storage class fixes a variable’s scope, lifetime and default initial value.
Automatic variables: local scope, exist only while the block runs, start as garbage.
Global variables: file scope, exist for the whole run, start at 0.
extern declares a global variable defined elsewhere without allocating memory.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Give two differences between local and global variables, and one reason to avoid overusing global variables.
Show worked answer
(1) Scope: a local variable can be used only inside the block or function where it is declared, while a global variable can be used by every function that follows its definition. (2) Lifetime and initial value: a local variable is created each time the function is entered, starts with garbage, and is destroyed on exit; a global variable exists for the whole run and starts at 0. Reason to avoid overuse: any function can change a global variable, so when it holds a wrong value it is difficult to find which function changed it, and functions that depend on globals are harder to reuse and test.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/functions/scope-lifetime-and-the-auto-and-extern-storage-classes/ to read this page.