Extern in C

The extern keyword in C is used when a variable or function is declared in one place but used in another place. This becomes very important when a C program is split across multiple files. In small examples, everything may sit in one source file, but in real projects code is divided into headers and source files, and extern helps connect them correctly.

Many beginners get confused because extern is related to global variables, declarations, definitions, and linkage. If these ideas are mixed up, linker errors and duplicate definition problems appear very quickly. In this article, we will understand extern in C, how it works, when to use it, how it behaves across files, and common mistakes.

What is Extern in C?

extern in C is a keyword used to declare that a variable or function exists somewhere else, usually in another source file or later in the program.

extern tells the compiler, “this name exists, but the actual storage or full definition is provided elsewhere.”

It is commonly used with global variables shared across multiple files. It can also appear with function declarations, although function declarations already have external linkage by default unless static is used.

Why Extern is Needed in C

C programs are often divided into multiple source files. If one file defines a global variable and another file wants to use it, the second file must know that the variable exists. That is where extern is useful.

  • to share global variables across source files
  • to separate declaration from definition
  • to organize large projects cleanly
  • to expose interfaces through header files
  • to avoid creating multiple independent copies of the same global object

Extern Variable Syntax in C

The common syntax is:

extern data_type variable_name;

Example:

extern int counter;

This does not normally create storage for counter. It only declares that a variable with that name and type exists somewhere else.

Declaration vs Definition with Extern in C

This is the most important part of the topic.

  • a declaration tells the compiler that a name exists
  • a definition creates the actual storage for the object

Example:

extern int counter;   /* declaration */
int counter = 0;      /* definition */

The first line says the variable exists. The second line creates storage and initializes it.

In practical multi-file code, there should usually be only one definition of a shared global variable, but there may be multiple matching extern declarations.

Extern in C Across Multiple Files

This is the most common real-world use of extern.

Header file:

/* shared.h */
extern int counter;

Source file that defines the variable:

/* shared.c */
int counter = 0;

Another source file that uses it:

/* main.c */
#include <stdio.h>
#include "shared.h"

int main(void) {
    counter++;
    printf("counter = %d\n", counter);
    return 0;
}

This works because main.c sees the declaration in the header, while the actual storage is provided once in shared.c.

Extern with Functions in C

extern can also be used with function declarations.

extern void showMessage(void);

But in standard C, this is usually optional because normal function declarations already have external linkage unless the function is marked static.

So these two declarations usually mean the same thing:

void showMessage(void);
extern void showMessage(void);

For variables, extern matters a lot more than it does for ordinary function declarations.

Extern vs Static in C

extern and static are often compared because they affect linkage in opposite ways.

KeywordMeaningLinkage Effect
externName refers to an externally linked object or function declared elsewhereAllows access across translation units when properly defined
staticName is limited to the current source file at file scopeGives internal linkage

If a file-scope variable is declared static, other source files cannot access it through extern.

Common Mistakes with Extern in C

  • defining the same global variable in more than one source file
  • using extern everywhere but forgetting the one real definition
  • placing the definition in a header file and causing multiple definitions after inclusion
  • expecting extern to work with a file-scope static variable
  • confusing declaration with definition

These mistakes usually produce linker errors or strange behavior in larger projects.

Best Practices for Extern in C

  • put shared extern declarations in header files
  • place the real variable definition in exactly one source file
  • avoid unnecessary global variables when a local design is possible
  • use clear names for shared objects so their role is obvious
  • use static instead of external linkage when the name should stay private to one file

FAQs

What is extern in C used for?

extern in C is mainly used to declare variables or functions that are defined elsewhere, especially across multiple source files.

Does extern create memory in C?

In the common case of declarations like extern int x;, it does not create storage. It only declares that the object exists elsewhere.

What is the difference between extern and static in C?

extern is used for names with external linkage that may be shared across files, while static at file scope limits visibility to the current source file.

Can extern be used with functions in C?

Yes, but it is usually optional for normal function declarations because functions already have external linkage by default unless they are declared static.