Course contentsShow
← C
Lesson 923 of 1,21913. Advanced CFree lesson

Storage class specifier precedence

Storage class specifier precedence

What you'll learn: Why you can only assign a single storage class specifier to a variable and how C enforces this structural boundary.

The One-Seat Rule

In C, storage class specifiers (auto, register, static, and extern) tell the compiler two things: where a variable lives (memory vs. CPU register) and how long it stays there. Think of a storage class like a seat on a bus. A passenger—your variable—can sit in the "Static Seat" or the "Register Seat," but they physically cannot sit in both at the same time.

Because these specifiers define mutually exclusive physical behaviors, C has a strict rule: you may use at most one storage class specifier in a single declaration. There is no "precedence" in the sense that one overrides the other; rather, the compiler treats the presence of a second specifier as a syntax error.

Why they can't mingle

Imagine trying to declare a variable as both static and extern.

  • static tells the compiler: "Keep this variable hidden inside this specific file."
  • extern tells the compiler: "Look for this variable in a different file."

These instructions are polar opposites. If the compiler allowed both, it wouldn't know whether to hide the variable or go searching for it. Similarly, register asks to put a variable inside the CPU for speed, while static requires a fixed address in RAM to persist for the life of the program. A variable cannot exist in high-speed temporary circuitry and permanent long-term memory simultaneously.

Code: The Conflict

If you try to mix these, your compiler will stop you immediately. It doesn't try to guess which one you want more; it simply refuses to build.

#include <stdio.h>

int main() {
    // ERROR: Multiple storage classes
    // The compiler will throw an error like "multiple storage classes in declaration"
    // static register int fastAndPermanent = 10; 

    // VALID: Only one specifier allowed
    static int permanent = 50;
    register int fast = 5;

    printf("%d %d\n", permanent, fast);
    return 0;
}

The "Typedef" Exception

Interestingly, C treats the typedef keyword technically as a storage class specifier for syntactic reasons. This means you also cannot combine typedef with static or extern. You are defining a type, or you are defining a variable with a specific storage behavior—you cannot do both in one line.

Key takeaway

A declaration can have only one storage class specifier because they define conflicting physical locations and lifetimes for data.