0Pricing
C Academy · Lesson

extern and Linkage

Share symbols across files.

extern and Linkage is a free C Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Linkage?

Linkage describes whether a name can be shared between different source files.

A name with external linkage refers to the same entity across the whole program. A name with internal linkage is private to its own file. Understanding linkage is key to multi-file programs.

Sharing a Variable

Suppose two files need access to one global counter. You cannot simply define it in both files; that creates two separate variables and a duplicate-symbol link error.

Instead, you define it once and declare it everywhere else with extern.

/* config.c */
int g_count = 0;   /* the one definition */

The extern Keyword

extern tells the compiler that a variable exists somewhere else in the program, without creating storage for it here.

It is a pure declaration. The linker later resolves it to the single definition in another file.

/* other.c */
extern int g_count;   /* declared, not defined */

void bump(void) {
    g_count++;
}

Declaration in the Header

The clean pattern: put the extern declaration in a header, and the matching definition in exactly one source file.

Any file that includes the header can then use the shared variable.

/* config.h */
#ifndef CONFIG_H
#define CONFIG_H
extern int g_count;
#endif

One Definition, Many Declarations

This is the One Definition Rule for globals: a variable may be declared with extern in many files, but defined in only one.

The single definition (without extern) allocates the storage. All the extern declarations just point to it.

/* config.h */ extern int g_count;  /* declaration */
/* config.c */ int g_count = 0;     /* definition  */

Functions Have External Linkage Too

By default, functions also have external linkage, which is why a prototype in a header lets other files call them.

You may write extern before a prototype, but it is redundant; functions are external unless you say otherwise.

/* both forms mean the same */
int add(int a, int b);
extern int add(int a, int b);

Internal Linkage With static

The static keyword at file scope gives a name internal linkage: it is visible only inside its own source file.

Use it for helpers and globals that should stay private to a module. Other files cannot see or clash with them.

/* parser.c */
static int line_no = 0;   /* private to parser.c */

static void advance(void) {  /* private helper */
    line_no++;
}

extern vs static

These two keywords sit at opposite ends of linkage.

extern requests external linkage: share this name across files. static at file scope forces internal linkage: keep this name private. Choosing well controls what your module exposes.

extern int shared_total;   /* visible everywhere */
static int local_cache;    /* only this file */

A Common Mistake

Beginners sometimes put a plain definition like int g_count; in a header.

Because the header is included in several files, each file gets its own definition and the linker reports a duplicate symbol. Always declare with extern in the header, define once in a .c file.

/* WRONG in a header included twice */
int g_count;          /* multiple definition */
/* RIGHT */
extern int g_count;   /* declaration only */

Putting It Together

This three-file shape is the standard way to share state.

The header declares, one source defines, and others use it after including the header. The linker stitches all the references to the single definition.

/* config.h */ extern int g_count;
/* config.c */ #include "config.h"
               int g_count = 0;
/* main.c   */ #include "config.h"
               /* uses g_count */

Initialization Note

Only the real definition should give an initial value: int g_count = 0;.

An extern declaration must not initialize, since it allocates no storage. Writing extern int g_count = 0; turns it into a definition and defeats the purpose.

extern int g_count;       /* ok: declaration */
extern int g_count = 0;   /* becomes a definition */

Quick Check

Check your understanding of extern and linkage.

Recap

You learned that linkage controls cross-file visibility. extern declares a name defined elsewhere; static at file scope keeps a name private.

Share globals with one definition plus extern declarations. Next we will compile and link these files into a program.

Frequently asked questions

Is the “extern and Linkage” lesson free?

Yes — the full text of “extern and Linkage” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “extern and Linkage”?

Share symbols across files. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “extern and Linkage” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Header and Source Files
  2. Include Guards
  3. extern and Linkage
  4. Compiling Multiple Files
← Back to C Academy