Skip to main content
A global variable in C is a variable declared outside of all functions and blocks, possessing file scope and static storage duration. It is accessible to any function defined within the same translation unit from the point of its declaration downward.

Technical Characteristics

Scope and Visibility Global variables possess file scope. Their visibility begins at the exact line of declaration and extends to the end of the source file. If a local variable within a function shares the same identifier as a global variable, the local variable shadows the global variable within that block. Storage Duration and Lifetime Global variables have static storage duration. Memory for these variables is allocated once when the program is loaded into memory and is deallocated only when the program terminates. Their values persist across multiple function calls. Initialization Rules
  • Implicit Initialization: If not explicitly initialized by the programmer, the C compiler automatically initializes global variables to zero (0 for integers, 0.0 for floats, \0 for characters, and NULL for pointers).
  • Constant Expressions: Explicit initialization of a global variable must be done using a constant expression. You cannot initialize a global variable with a value evaluated at runtime, such as a function return value or another variable.
Linkage By default, global variables possess external linkage. This means a global variable defined in one source file can be referenced in another source file using the extern keyword.
To restrict a global variable’s visibility strictly to the translation unit in which it is defined, it must be declared with the static keyword, which changes its linkage from external to internal linkage. Memory Layout Global variables are stored in specific segments of the program’s virtual memory space, separate from the stack and heap:
  • Data Segment: Houses global variables that are explicitly initialized to a non-zero value.
  • BSS (Block Started by Symbol) Segment: Houses global variables that are either uninitialized or explicitly initialized to zero. The OS zeroes out this memory segment before program execution begins.
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More