Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Storage Classes & Visibility

Lesson 23 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Lifetime and Visibility

Storage classes are keywords that tell the C compiler four vital things about a variable:

  1. Storage Location: Where is it in RAM? (Stack, Heap, or Data Segment).
  2. Default Value: If you don't initialize it, is it zero or garbage?
  3. Scope: Who can see it? (Just one block, or the whole file?).
  4. Lifetime: How long does it stay in memory?

1. auto (The Default)

This is the default for any variable declared inside a function or a block.

  • Storage: Stack
  • Default: Garbage (random bits)
  • Lifetime: Only exists while the function is running.

2. static (The Persistent Variable)

A static variable is one of the most useful tools in C. It retains its value even after its function ends.

void increment() {
    static int count = 0; // Initialized only once!
    count++;
    printf("%d ", count);
}
// Calling this 3 times prints: 1 2 3

It is created once at the start of the program and lives until the program closes.

3. extern (The Global Linker)

Used to share a variable across multiple files. It tells the compiler: "I'm using this variable here, but its actual definition is in a different file."

4. register (The Speed Demon)

A hint to the compiler to store the variable in a CPU register (the fastest memory in the computer) instead of RAM.

  • Note: You cannot use the address-of operator (&) on a register variable because CPU registers don't have memory addresses.

Summary Table

KeywordStorageDefaultScopeLifetime
autoStackGarbageLocalBlock end
staticData SegZeroLocalProgram end
externData SegZeroGlobalProgram end
registerRegisterGarbageLocalBlock end

Practical Advice

Keep your variables as "Local" and "Short-lived" as possible. Global variables (extern) can be changed by any function anywhere, making it nearly impossible to track down which function caused a bug!

Use static variables when you need a function to "remember" something (like how many times it was called or a running total) without exposing that data to other functions.