> ## 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.

# C Extern Variable

The `extern` keyword in C is a storage class specifier used to declare a variable or function without providing its definition. It instructs the compiler that the identifier exists and its memory allocation or implementation is provided elsewhere, leaving an unresolved symbol reference in the object file for the linker to resolve during the final build phase.

## Syntax

```c theme={"dark"}
extern data_type variable_name;
extern return_type function_name(parameter_types);
```

## Core Mechanics

### 1. Declaration vs. Definition and Tentative Definitions

In C, a **definition** allocates memory for a variable, while a **declaration** only informs the compiler of the variable's name and type.

* An uninitialized global variable at file scope (e.g., `int x;`) is a **tentative definition**. It acts as a declaration that becomes a strict definition at the end of the translation unit if no explicit definition (e.g., `int x = 1;`) is provided.
* Applying `extern` without an initializer creates a pure declaration. It guarantees to the compiler that the variable exists and memory has been allocated for it in another translation unit or later in the same file.

### 2. Linkage Rules

While `extern` is generally used to specify **external linkage** (making the identifier accessible across multiple translation units), its exact behavior depends on prior declarations:

* If no prior declaration is visible, `extern` establishes external linkage.
* If a prior declaration of the identifier is visible with **internal linkage** (e.g., previously declared with the `static` keyword), the `extern` declaration inherits that internal linkage rather than establishing external linkage.

### 3. Storage Duration

Variables declared with `extern` typically possess **static storage duration**, meaning they are allocated when the program begins execution and deallocated when it terminates. However, since C11, if a variable is declared with `extern _Thread_local`, it possesses **thread storage duration**, meaning a unique instance of the variable is created for each thread.

### 4. Application to Functions

Functions in C possess external linkage by default. While explicitly applying `extern` to a function prototype (e.g., `extern void my_func();`) is redundant, it is perfectly valid, historically common, and explicitly supported by the C standard.

## Code Visualization

**File 1: `sourceA.c` (The Definitions)**

```c theme={"dark"}
// Memory is allocated here. 
// These are the definitive sources of truth for the linker.
int system_status = 1; 

void trigger_alert(void) {
    // Function implementation
}
```

**File 2: `sourceB.c` (The Declarations)**

```c theme={"dark"}
// Pure declarations. No memory is allocated.
// The compiler allows the use of these identifiers based on these signatures.
extern int system_status; 
extern void trigger_alert(void); 

void check_status(void) {
    if (system_status == 1) {
        // The linker resolves system_status and trigger_alert to sourceA.c
        trigger_alert();
    }
}
```

## Technical Caveats

* **Initialization Overrides `extern`:** If an `extern` variable is initialized at the time of its declaration, the compiler treats it as a strict definition. Memory is allocated, and the `extern` keyword is effectively ignored as a pure declaration.

```c theme={"dark"}
extern int count = 100; // WARNING: This is a definition, not a pure declaration.
```

If this occurs in a header file included by multiple translation units, the linker will throw a "multiple definition" error.

* **Missing Definition:** If an `extern` variable or function is declared and utilized in the code, but never defined in any linked translation unit, the compilation phase will succeed, but the linker will fail with an "undefined reference" or "unresolved external symbol" error.
* **Type Matching:** The data type in the `extern` declaration must strictly match the data type of the actual definition. The C linker does not perform type checking; mismatched types will result in undefined behavior due to incorrect memory offset calculations or register usage.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
