When to use extern in C/C++

In C, the extern keyword is used to declare a variable or function that is defined elsewhere in the program. It tells the compiler that the variable or function exists, but its actual definition (memory allocation) is in another file or later in the same file.

Syntax

// Variable declaration using extern
extern datatype variable_name;

// Function declaration using extern (optional)
extern return_type function_name(parameters);

Parameters:

  • datatype − The data type of the variable (int, char, float, etc.)
  • variable_name − Name of the variable to be declared
  • return_type − Return type of the function
  • function_name − Name of the function to be declared

When to Use extern

The extern keyword is primarily used in the following scenarios −

  • Sharing global variables across multiple files
  • Forward declaration of variables defined later in the program
  • Accessing variables from external libraries
Note: To demonstrate multi-file examples, create separate .c files and compile them together using: gcc file1.c file2.c -o program

Method 1: Accessing Global Variables in Same File

You can use extern to access global variables that are defined later in the same file −

#include <stdio.h>

int main() {
    extern int globalVar;  // Declaration
    printf("Global variable value: %d\n", globalVar);
    return 0;
}

int globalVar = 42;  // Definition
Global variable value: 42

Method 2: Sharing Variables Between Functions

Here's an example demonstrating variable sharing within the same program −

#include <stdio.h>

int x = 100;
int y = 200;

void displayValues() {
    extern int y;  // Optional declaration
    printf("x = %d, y = %d\n", x, y);
}

int main() {
    printf("Initial values:\n");
    displayValues();
    
    x = 150;
    y = 250;
    
    printf("Modified values:\n");
    displayValues();
    
    return 0;
}
Initial values:
x = 100, y = 200
Modified values:
x = 150, y = 250

Method 3: Using extern for Function Declarations

Functions are extern by default, but you can explicitly declare them for clarity −

#include <stdio.h>

extern int add(int a, int b);  // Function declaration

int main() {
    int result = add(10, 20);
    printf("Sum: %d\n", result);
    return 0;
}

int add(int a, int b) {  // Function definition
    return a + b;
}
Sum: 30

Key Points

  • extern provides declaration without definition
  • Memory is allocated only when the variable is defined, not declared
  • Functions are extern by default in C
  • Useful for organizing large programs across multiple files

Common Mistakes

  • Declaring extern variable without defining it anywhere causes linker errors
  • Initializing an extern variable makes it a definition, not just a declaration

Conclusion

The extern keyword is essential for sharing variables and functions across files in C. It separates declaration from definition, enabling better code organization and modularity in large programs.

Updated on: 2026-03-15T10:01:51+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements