ProgrammingC Developer

Explain the rules and nuances of declaring and initializing variables in C, including auto-initialization for different storage areas. How do you distinguish between definition and declaration, and why is it important?

Pass interviews with Hintsage AI assistant

Answer.

Declaring and initializing variables is a cornerstone of the C language with very strict and sometimes non-obvious rules. How and where you declare a variable can affect its initial value (concerning initialization) as well as its association with a memory object (concerning declaration and definition).

Background

C dates back to times when memory optimization was a priority. Developers had to manually declare and initialize variables; otherwise, the program's behavior became unpredictable. In modern C compilers, even small deviations can lead to linker errors or implicit initialization with "garbage".

Problems

Common errors:

  • Automatic variables are uninitialized by default
  • static and global ones are auto-initialized to zero
  • Confusion between declaration and definition, leading to multiple definitions or undefined symbols

Solutions

  • Automatic (auto, by default local) variables: they are not initialized!
  • Static (function static, file static, globals without extern): auto-initialization to zero if no value is assigned.
  • Declaration: informs the compiler of the existence of a variable — no memory is reserved (example: extern int x;)
  • Definition: reserves memory and may (or must) be initialized (example: int x = 42;)

Code example:

#include <stdio.h> int global_var; // definition, auto-initialization = 0 static int static_global_var; // static file, auto-initialization = 0 extern int extern_var; // declaration, definition somewhere else void foo() { int local_var; // automatic, uninitialized -> garbage static int static_local_var; // static, auto-initialized to 0 }

Key points:

  • Variable location affects auto-initialization
  • Definition and declaration are not the same!
  • extern/no extern: for variables — about declaration/definition, for functions — only declaration

Trick questions.

1. Are automatic variables (local without static and extern) automatically initialized to 0 by the compiler?

No, they contain garbage. Their value is undefined; using them before initialization is an error.

2. Can you define a variable with extern multiple times in different files?

No, there should be one definition, with the rest being declarations via extern; otherwise, the linker will issue "multiple definition" or "undefined symbol" errors.

3. What is the difference between a function declaration and its definition?

A declaration is just a prototype (without a body); a definition must include the function's body. For variables, a declaration via extern does not reserve memory, while both forms are allowed for functions.

Common mistakes and anti-patterns

  • Expectation of auto-initialization for local variables
  • Multiple definitions of global variables in header files
  • Incorrect use of extern

Real-life example

Negative case

A global variable int counter; is declared in two header files. The project linked with a "multiple definition" error.

Pros:

  • Quick code copying

Cons:

  • Linker errors, unclear compiler messages

Positive case

In the header, extern int counter; is declared; definition int counter = 0; — only in one C file.

Pros:

  • Clean compilation, logical structure

Cons:

  • Need to remember about separating declaration/definition