ProgrammingEmbedded C Developer

What are the differences between variables with storage classes auto, static, and extern in the C language, and how does this affect their lifecycle and accessibility?

Pass interviews with Hintsage AI assistant

Answer.

In the C language, the storage class of a variable determines where the data is stored, how long it is accessible, and which area of code can refer to it. Historically, the keywords auto (default for local variables), static (retains value between calls, often used to store state), and extern (declares a variable defined elsewhere) have been introduced for controlling the visibility and lifetime of variables.

Issue — misunderstanding where and how long a variable lives may lead to access errors, memory leaks, and hard-to-read code. For example, mistakenly expecting a local static variable to be recreated on each function call, or conversely, that an auto variable will retain its value between calls.

Solution — always consciously choose a storage specifier and understand its implications:

  • auto is usually not needed (is the default),
  • static for retaining value between calls or for limiting visibility within a module,
  • extern for accessing global variables defined in other files.

Example usage:

// main.c int global_var = 42; // has static storage class by default, external linkage void func() { static int counter = 0; // lives between calls auto int temp = 5; // local, auto does not need to be specified counter++; printf("call #%d\n", counter); } extern int global_var;

Key features:

  • auto: variable lives until the end of the block (scope) in which it was declared.
  • static: variable lives throughout the program but is only visible within the file/function/block.
  • extern: variable is declared but not defined here; its definition is in another file.

Trick questions.

Why even write auto, if variables are auto by default?

Answer: In modern versions of C, the keyword auto is rarely used explicitly — for local variables it is the default specifier. In general, its explicit writing offers no advantages.

Can static be used inside a function to declare a global variable?

Answer: No, static inside a function makes the variable local but state-preserving between calls. It is not visible outside the function.

Code example:

void foo() { static int call_count = 0; // Not global, but lives between calls call_count++; }

What happens if a variable is declared as extern inside a function but not defined anywhere?

Answer: This will lead to a linker error because a reference to a global variable that does not exist is declared.

Typical mistakes and anti-patterns

  • Confusing scope and lifetime (for example, expecting static to be local outside the function).
  • Declaring extern variables without definitions.
  • Using auto without necessity.

Example from life

Negative case

In a large project, module variables were declared as extern in all source files but forgot to provide definitions. As a result — mysterious linking errors, unclear for novice developers.

Pros:

  • Allowed referencing variables across many files.

Cons:

  • Hard to maintain.
  • Errors that arise only after compiling all files, not at the coding stage.

Positive case

Strictly defined scope: each static variable only in the necessary module, global extern declared in headers and defined in a single location.

Pros:

  • Clear architecture.
  • Reduced interdependencies, ease of maintenance.

Cons:

  • If organized incorrectly — possible excessive fragmentation of variables.