0Pricing
C Academy · Lesson

extern and Global

Sharing across files.

extern and Global 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.

Global Variables

A variable declared outside any function has file scope and lives for the whole program. It is a global.

#include <stdio.h>
int counter = 0;
void bump(void) {
    counter++;
}
int main(void) {
    bump();
    bump();
    printf("counter = %d\n", counter);
    return 0;
}

Globals Are Shared

Every function in the file can read and write a global. This makes them convenient but easy to misuse.

#include <stdio.h>
int score = 100;
void penalty(void) { score -= 25; }
void bonus(void) { score += 10; }
int main(void) {
    penalty();
    bonus();
    printf("score = %d\n", score);
    return 0;
}

Default Zero Initialization

Globals without an initializer start at 0 automatically.

#include <stdio.h>
int total;
int main(void) {
    printf("total = %d\n", total);
    return 0;
}

The extern Keyword

extern declares a global that is defined elsewhere. It tells the compiler the variable exists without allocating it.

#include <stdio.h>
int shared = 5;
extern int shared;
int main(void) {
    printf("shared = %d\n", shared);
    return 0;
}

Declaration vs Definition

A definition allocates storage. A declaration just names something. extern declarations do not allocate.

#include <stdio.h>
int value = 42;
int main(void) {
    extern int value;
    printf("value = %d\n", value);
    return 0;
}

Sharing Across Files

In multi-file projects, one .c file defines the global and other files use extern in a header to see it.

#include <stdio.h>
extern int globalCount;
int globalCount = 3;
int main(void) {
    printf("globalCount = %d\n", globalCount);
    return 0;
}

Functions Can Be extern Too

Functions have external linkage by default, so a prototype is effectively an extern declaration of that function.

#include <stdio.h>
extern void hello(void);
void hello(void) {
    printf("Hello from extern\n");
}
int main(void) {
    hello();
    return 0;
}

Global Lifetime

Globals exist from program start to finish, so their values persist across all function calls.

#include <stdio.h>
int visits = 0;
void page(void) { visits++; }
int main(void) {
    page();
    page();
    page();
    printf("visits = %d\n", visits);
    return 0;
}

Risks of Globals

Because any code can change a global, bugs become hard to trace. Prefer passing values as parameters when possible.

#include <stdio.h>
int config = 1;
int compute(int input) {
    return input * config;
}
int main(void) {
    printf("%d\n", compute(8));
    return 0;
}

Read-Only Globals

Combine const with a global to make a shared constant that no one can modify.

#include <stdio.h>
const int MAX_USERS = 50;
int main(void) {
    printf("MAX_USERS = %d\n", MAX_USERS);
    return 0;
}

Practical Pattern

A global config value read by several functions is a common, acceptable use.

#include <stdio.h>
int multiplier = 3;
int scale(int x) { return x * multiplier; }
int main(void) {
    printf("%d\n", scale(4));
    printf("%d\n", scale(7));
    return 0;
}

Quick Check

Test your understanding of extern and globals.

Recap: extern and Global

You learned about globals and external linkage.

  • Globals are file-scope variables with program lifetime
  • They default to 0 and are shared by all functions
  • extern declares a global defined elsewhere
  • Definition allocates, declaration only names
  • Use globals sparingly to avoid hidden coupling

Frequently asked questions

Is the “extern and Global” lesson free?

Yes — the full text of “extern and Global” 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 Global”?

Sharing 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 Global” 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. auto and Local Scope
  2. static Variables
  3. extern and Global
  4. register and const
← Back to C Academy