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%

Unit 2 — Storage Classes

Lesson 11 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Storage Classes in C

A storage class defines the scope (visibility), lifetime (duration), and default initial value of a variable.

---

The Four Storage Classes

1. auto

  • Default storage class for local variables
  • Scope: local to the block
  • Lifetime: until the block ends
  • Default value: garbage
auto int x = 10;   /* same as: int x = 10; */

2. register

  • Requests that the variable be stored in a CPU register (faster access)
  • Cannot take the address of a register variable
  • Scope: local to block
  • Lifetime: until block ends
register int counter;
for (register int i = 0; i < 1000000; i++) {
    counter++;
}
Note: The compiler may ignore the register hint.

3. static

  • Retains value between function calls
  • Global static: scope restricted to file
  • Local static: scope local, but lifetime = entire program
void countCalls() {
    static int count = 0;   /* initialised only once */
    count++;
    printf("Called %d times\n", count);
}

int main() {
    countCalls();  /* Called 1 times */
    countCalls();  /* Called 2 times */
    countCalls();  /* Called 3 times */
}

4. extern

  • Declares a variable defined in another file or later in the same file
  • Used to share variables across multiple files

File 1 (main.c):

#include <stdio.h>
extern int sharedVar;   /* declaration */

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

File 2 (data.c):

int sharedVar = 42;   /* definition */

---

Comparison Table

Storage ClassScopeLifetimeDefault ValueStorage
autoLocalBlockGarbageStack
registerLocalBlockGarbageCPU Register
staticLocal/FileProgram0Data segment
externGlobalProgram0Data segment