C static and extern

Beginner
8 min read
Updated: Nov 2026
2 examples

What Is static and extern?

static has two jobs. Inside a function it makes a variable persist between calls. At file scope it gives a variable or function internal linkage so other files cannot see it. extern declares a name that is defined in another file, which is how files share globals.

Remember
int next_id(void) {
    static int id = 0;     /* persists between calls */
    return ++id;
}
static int helper(void);    /* private to this file */
extern int max_items;       /* defined in another .c file */

Cheat Sheet

Keep these facts handy while you work with static and extern in C.

TopicDetail
static localInitialized once, keeps its value
static globalInternal linkage
static functionPrivate helper
externDeclaration, not a definition

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Recursion.

Example 1 — A static counter for unique IDs

Each call continues from the previous value.

C
#include <stdio.h>

int next_id(void) {
    static int id = 0;
    return ++id;
}

int main(void) {
    for (int i = 0; i < 3; i++) {
        printf("id %d\n", next_id());
    }
    return 0;
}

How It Works

  1. static int id = 0; runs its initializer once, before main.
  2. Each call increments the same stored value.
  3. No global variable is needed, so nothing else can change id.

Example 2 — Sharing a global with extern

Build with gcc main.c config.c -o app.

C
/* config.c */
int max_items = 5;

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

extern int max_items;

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

How It Works

  1. config.c defines max_items once.
  2. extern int max_items; in main.c only declares it.
  3. The linker connects the two. Put the extern declaration in a shared header.

Edge Cases & Pitfalls

Check these before relying on static and extern in real programs.

init

extern with an initializer

extern int x = 5; at file scope defines x. Define once, declare elsewhere.

static

Static in headers

A static variable in a header creates a separate copy in each file that includes it.

Key Takeaways

  • Persist: static locals keep their value.
  • Private: file-scope static hides names.
  • Share: extern links to another file's definition.

One line: static has two jobs.

Frequently Asked Questions

Inside a function it makes the variable keep its value between calls. At file scope it makes the name private to that source file.
extern declares a variable or function defined in another translation unit so several files can share it.
Continue with "C Recursion" next. Functions that call themselves, with a base case to stop. The C Functions hub lists every topic in order.

Next: Recursion

Functions that call themselves, with a base case to stop.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful