C Lifetime and Linkage

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Lifetime and Linkage?

Storage duration (lifetime) says how long an object exists: automatic (until the block ends), static (the whole program), or allocated (until free). Linkage says whether the same name in different files refers to the same object: external, internal (static), or none.

Remember
int g;               /* static duration, external linkage */
static int h;        /* static duration, internal linkage */
void f(void) {
    int a;           /* automatic duration, no linkage */
    static int s;    /* static duration, no linkage */
}

Cheat Sheet

Keep these facts handy while you work with Lifetime and Linkage in C.

TopicDetail
AutomaticLocals; destroyed at block end
StaticExists for the whole program, zero-initialized
External linkageShared across files
Internal linkagestatic at file scope

C Examples

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

Example 1 — Automatic versus static duration

The static counter remembers its value between calls.

C
#include <stdio.h>

void tick(void) {
    int a = 0;
    static int s = 0;
    a++;
    s++;
    printf("auto=%d static=%d\n", a, s);
}

int main(void) {
    tick();
    tick();
    tick();
    return 0;
}

How It Works

  1. a is recreated and reset every call.
  2. s is initialized once and keeps its value.
  3. Both have no linkage because they are local.

Edge Cases & Pitfalls

Check these before relying on Lifetime and Linkage in real programs.

dangling

Using an object after its lifetime

Returning the address of an automatic variable gives a dangling pointer.

link

Accidental sharing

A file-scope variable without static is visible to every file and can clash. Make private data static.

Key Takeaways

  • Lifetime: automatic, static, or allocated.
  • Linkage: external, internal, or none.
  • Default: file-scope data is external unless static.

One line: Storage duration (lifetime) says how long an object exists: automatic (until the block ends), static (the whole program), or allocated (until free).

Frequently Asked Questions

Scope is where a name is visible in the source. Lifetime is how long the object exists at run time. A static local has block scope but program lifetime.
A name with internal linkage (declared static at file scope) is visible only within its translation unit, so other files cannot link to it.
Continue with "C Storage Classes" next. auto, register, static, and extern at a glance. The C Functions hub lists every topic in order.

Next: Storage Classes

auto, register, static, and extern at a glance.

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.

10 people found this page helpful