C Storage Classes

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Storage Classes?

A storage class specifier controls lifetime, linkage, and where an object may live. C has auto, register, static, extern, and (C11) _Thread_local. Only one storage class may appear in a declaration.

Remember
auto int a;        /* default for locals        */
register int i;    /* hint: keep in a register  */
static int s;      /* persists / file-private   */
extern int g;      /* defined in another file   */

Cheat Sheet

Keep these facts handy while you work with Storage Classes in C.

TopicDetail
autoDefault for locals; rarely written
registerHint; cannot take its address
staticPersistent local or file-private name
externRefers to an object defined elsewhere

C Examples

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

Example 1 — Static, extern, and automatic together

A file-private variable and an extern view of a shared one.

C
#include <stdio.h>

int shared = 10;            /* external linkage */
static int hidden = 20;     /* internal linkage */

void demo(void) {
    extern int shared;      /* refers to the global above */
    int local = 1;          /* auto */
    shared++;
    printf("shared=%d hidden=%d local=%d\n", shared, hidden, local);
}

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

How It Works

  1. shared keeps its increment across calls.
  2. hidden is visible only in this file.
  3. local is automatic and restarts at 1 each call.

Edge Cases & Pitfalls

Check these before relying on Storage Classes in real programs.

one

More than one specifier

static extern int x; is invalid; choose a single storage class.

reg

Taking the address of register

&i on a register variable is a compile error.

Key Takeaways

  • auto: default for locals.
  • static: lifetime or privacy.
  • extern: link to a definition elsewhere.

One line: A storage class specifier controls lifetime, linkage, and where an object may live.

Frequently Asked Questions

C has four classic ones: auto, register, static, and extern. C11 adds _Thread_local.
auto. You almost never write it explicitly.
Continue with "C auto and register" next. The default local storage class and the register hint. The C Functions hub lists every topic in order.

Next: auto and register

The default local storage class and the register hint.

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.

11 people found this page helpful