Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
#include <stdio.h>

/* Global variable explicitly initialized */
int g_active_connections = 5; 

/* Global variable implicitly initialized to 0 */
int g_error_flag; 

void update_state(void) {
    /* Accessing and modifying global variables */
    g_active_connections++;
    g_error_flag = 1;
}

int main(void) {
    update_state();
    return 0;
}

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.
int g_max = 100;               /* Valid: Constant expression */
int g_limit = g_max + 50;      /* Invalid in C: g_max is not a compile-time constant */
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.
/* file1.c */
int g_shared_data = 42; /* Definition */

/* file2.c */
extern int g_shared_data; /* Declaration referencing the variable in file1.c */
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.
Master C with Deep Grasping Methodology!Learn More