MCS-201 · Unit 5

Functions

Section 5 of 8

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

5.5Static and Register Variables

Covers IGNOU MCS-201 §5.6.3, 5.6.4

Two storage classes remain. static gives a local variable a memory that survives between calls, which is useful for counters and “has this run before?” flags. register is a historical hint for speed that still appears in examinations.

Static local variables

Properties of a static variable:

  • Scope: local, like an automatic variable; other functions cannot use its name.
  • Lifetime: the whole program run.
  • Initialisation: done only once, before the program starts, and the initial value must be a constant expression. Without an initialiser, it starts at 0.

Register variables

A CPU register is a very small, very fast storage location inside the processor. The register storage class asks the compiler to keep a heavily used local variable in a register instead of main memory:

Syntax
register int i;
PropertyRegister variable
Scope and lifetimeSame as an automatic variable
Default initial valueGarbage
AddressThe & operator cannot be applied, so no pointer to it
GuaranteeNone: it is only a request, and the compiler may ignore it
Typical useLoop counters

Comparison of the four storage classes

Featureautoextern (global)static (local)register
DeclaredInside a blockOutside all functionsInside a block with staticInside a block with register
ScopeBlockFile (program)BlockBlock
LifetimeBlock executionWhole programWhole programBlock execution
Default valueGarbage00Garbage
Keeps value between callsNoYesYesNo
& allowedYesYesYesNo

Key takeaways

  • static local variables: local scope, whole-program lifetime, initialised once, default 0.
  • They are used for values that must persist between calls, such as counters.
  • register requests fast storage; it is a hint, and & cannot be used on such a variable.
  • auto and register variables start as garbage; extern and static variables start at 0.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many times is static int count = 0; inside a function initialised when the function is called 5 times?

Show answer

Answer: C. 1

A static variable is initialised once only, before the program starts running; later calls reuse its current value.

Question 2 · multiple choice

What does this program print?

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

void visit(void)
{
    static int n = 0;
    n++;
    printf("%d ", n);
}

int main(void)
{
    visit();
    visit();
    visit();
    printf("\n");
    return 0;
}
Show answer

Answer: B. 1 2 3

n keeps its value between calls, so it becomes 1, 2 and 3. Without static the output would be 1 1 1.

Question 3 · multiple choice

Which operation is not allowed on a register variable?

Show answer

Answer: D. Taking its address with &

A register variable may not have a memory address, so & is forbidden. All ordinary arithmetic is allowed.

Question 4 · multiple choice

What is the default initial value of static float rate; inside a function?

Show answer

Answer: A. 0.0

Static variables are zero-initialised when no initial value is given. Garbage applies to auto and register variables.

Question 5 · multiple choice

Which storage class gives a variable local scope and whole-program lifetime?

Show answer

Answer: C. static

A static local variable is visible only in its block but exists for the entire run. extern variables have file scope.

Question 6 · short answer

Write a function next_id that returns 101 on its first call, 102 on its second, and so on, without using a global variable. Explain why it works.

Show worked answer

int next_id(void) { static int id = 100; id++; return id; }. The variable id is static, so it is created and set to 100 once only. Each call adds 1 and returns the new value: the first call returns 101, the second finds id still 101 and returns 102, and so on. Because id is local, no other function can change it accidentally, which is the advantage over a global variable.

↑ ↓ move↵ openTab switch type