MCS-201 · Unit 5

Functions

Section 4 of 8

Block 2 · Unit 5Functions10 min · Section 4 of 8

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 classKeywordScopeLifetimeDefault initial value
Automaticauto (optional)The block where it is declaredUntil that block endsGarbage (indeterminate)
ExternalexternFrom its declaration to the end of the file; all files if declared thereThe whole program run0
Static (local)staticThe block where it is declaredThe whole program run0
RegisterregisterThe block where it is declaredUntil that block endsGarbage (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.

DefinitionDeclaration with extern
Formint gv = 10; outside all functionsextern int gv;
Allocates memoryYesNo
May have an initial valueYes (a constant expression)No
Needed whenOnce per programThe 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 1 · multiple choice

What is the default initial value of an uninitialised global int?

Show answer

Answer: B. 0

Global (and static) variables start at 0. Garbage applies to automatic and register variables.

Question 2 · multiple choice

What does “lifetime” of a variable mean?

Show answer

Answer: C. The time during execution for which it exists in memory

Lifetime is about duration in memory; the region of code where the name is usable is its scope.

Question 3 · multiple choice

Which storage class is the default for a variable declared inside a function?

Show answer

Answer: D. auto

Local variables are automatic unless another storage class is specified.

Question 4 · multiple choice

What does this program print?

C · shadow.c
#include <stdio.h>

int n = 5;

void show(void)
{
    int n = 20;
    printf("%d ", n);
}

int main(void)
{
    show();
    printf("%d\n", n);
    return 0;
}
Show answer

Answer: C. 20 5

Inside show, the local n hides the global one, so 20 is printed; main sees only the global n, which is still 5.

Question 5 · multiple choice

Which statement about extern int total; is correct?

Show answer

Answer: A. It declares a variable defined elsewhere and allocates no memory

An extern declaration only tells the compiler the variable exists; the single definition elsewhere allocates the storage.

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.

↑ ↓ move↵ openTab switch type