ProgrammingEmbedded C Developer

Describe the mechanism of variable scope and lifetime in C. How do they depend on storage type, and what errors can arise from misunderstandings of these mechanisms?

Pass interviews with Hintsage AI assistant

Answer.

Scope and lifetime of variables are among the key aspects of C program structure. Scope is the part of the code where a variable is accessible by name. Lifetime defines when a variable actually exists in memory.

Background
C was designed for low-level control, thus providing a flexible but dangerous approach to scope and lifetime through the classification of variables by declaration place (block, file, global, static).

Problem
Misunderstanding of scope/lifetime leads to classic bugs: attempts to access unavailable or already destroyed variables (use-after-free), name conflicts between global and local variables (shadow variables), unintentional changes to global variables.

Solution
Explicitly define the needed storage type (auto, static, extern), wisely use block scope, minimize the number of global variables, and clearly differentiate between stack and non-stack lifetime.

Example code:

int global_var; // Global, lives for the entire runtime void func() { int local_var = 5; // Automatic, lives within func() static int stat_var = 0; // Static, lives between calls stat_var++; }

Key features:

  • Block scope of local variables: variables are accessible only within the block where they are declared.
  • Global variables live for the entire program runtime and are visible from any file when declared extern (unless static).
  • Static local variables retain their value between function calls but are only accessible within the function.

Trick questions.

What happens if you declare two variables with the same name in different blocks?

The inner variable will shadow the outer one (shadow variable). This can lead to unexpected errors.

int x = 10; ... if (1) { int x = 50; printf("%d", x); // prints 50, global x is hidden }

What is the lifetime of an automatic variable defined inside a function?

It exists only during the function call. After exit, the memory is freed, and the value is lost.

Can a static local variable be used outside the function where it was declared?

No, its scope is only within the function. It is not visible from outside, despite having a lifetime that lasts for the entire program execution.

void f() { static int x = 0; } // Not accessible outside f()

Common mistakes and anti-patterns

  • Using local variables after exiting their block.
  • Incorrect assumptions about variable lifetime (static vs auto).
  • Excessive use of global variables.

Real-life examples

Negative case

A novice developer creates a counter inside a loop as static, and this counter "accumulates" values between iterations, although it was expected to reset each time.

Pros:

  • Can explore the behavior of the variable while keeping state.

Cons:

  • Algorithm logic is violated, hard to debug.

Positive case

A developer uses static strictly for caching, and for temporary needs — regular auto variables.

Pros:

  • The code is transparent and predictable in behavior.

Cons:

  • Each storage type requires separate attention during refactoring.