Storage Classes in C

A Storage Class in C programming defines the scope (visibility), lifetime (longevity), memory storage location, and default initial value of variables and functions. Every variable in C possesses both a data type (specifying what kind of data it holds) and a storage class (specifying where and how long it resides in memory).

The 4 Essential Properties Defined by a Storage Class

 Storage Location: Where the variable is physically placed — CPU Register, RAM Stack segment, or RAM Data/BSS segment.

 Default Initial Value: The value automatically assigned if uninitialized (Garbage value vs. Zero 0).

 Scope: The region of code where the variable is visible and accessible (Block scope vs. File scope).

 Lifetime: The duration for which the variable retains its allocated memory during program execution.

Master Comparison Table of Storage Classes (BCA Exam Goldmine)

Storage Class

Keyword

Storage Location

Default Value

Scope

Lifetime

Automatic

auto

RAM (Stack)

Garbage

Local to block {}

Within enclosing block

Register

register

CPU Register

Garbage

Local to block {}

Within enclosing block

Static

static

RAM (Data segment)

Zero (0)

Local to block/file

Entire program execution

External

extern

RAM (Data segment)

Zero (0)

Global (all project files)

Entire program execution

1. Automatic Storage Class (auto)

All variables declared inside a function or block without any storage class prefix are automatic by default. The auto keyword is rarely written explicitly:


void sample() {
    auto int x = 10; /* Exactly equivalent to: int x = 10; */
}
							

2. Register Storage Class (register)

Requests the compiler to store the variable in a high-speed CPU register rather than in main RAM to accelerate loop iterations and arithmetic computations:


register int counter;
for(counter = 0; counter < 1000000; counter++) {
    /* Fast loop execution directly inside CPU */
}
							

 Crucial Restriction: You cannot obtain the memory address of a register variable using the address-of operator (&counter), because registers do not reside at RAM memory addresses!

3. Static Storage Class (static)

A static local variable is initialized only once and persists its value across multiple invocations of its function:


#include <stdio.h>

void incrementCounter() {
    static int count = 0; /* Initialized only once */
    count++;
    printf("Count = %d\n", count);
}

int main() {
    incrementCounter(); /* Output: Count = 1 */
    incrementCounter(); /* Output: Count = 2 */
    incrementCounter(); /* Output: Count = 3 */
    return 0;
}
							

4. External Storage Class (extern)

The extern keyword is used to declare a global variable defined in another source file or later in the same file, enabling multi-file modular C programming:


/* In File1.c */
int global_score = 100; /* Definition */

/* In File2.c */
extern int global_score; /* Declaration referencing File1.c */
void display() {
    printf("Score: %d\n", global_score);
}
							

Practice Session for Beginner Students

Master these frequently tested storage class problems and university viva questions.

Part 1: Spot the Bug & Predict the Output

Problem 1: What happens when you compile this C program?


#include <stdio.h>
int main() {
    register int x = 10;
    int *ptr = &x;
    printf("%d", *ptr);
    return 0;
}
							

Answer & Explanation: Compile-time Error: “address of register variable 'x' requested”. CPU registers are internal processor storage units, not addressable cells in RAM. Therefore, using & on a register variable is strictly illegal in C.

Part 2: Multiple Choice Questions (BCA Exam Preparation)

Q1. What is the default storage class for a variable declared inside a function?
A) static
B) auto
C) extern
D) register
Correct Answer: B) auto.

Q2. What is the default initial value of an uninitialized static variable?
A) Garbage value
B) 0
C) -1
D) NULL
Correct Answer: B) 0.

Q3. Which storage class is used to share a global variable across multiple C source files?
A) auto
B) register
C) static
D) extern
Correct Answer: D) extern.

Part 3: Hands-on Lab Assignments for Beginners

Assignment 1: Write a C program containing a function that counts and prints how many times it has been called during the program execution, using a static variable.

Part 4: Frequently Asked University Viva Questions

1. What is the difference between a global variable and a static global variable?
Answer: A standard global variable can be accessed from other files using extern. A static global variable restricts its visibility strictly to the file in which it is defined (internal linkage), preventing naming collisions across files.

2. Can a compiler ignore the register keyword?
Answer: Yes. The register keyword is only a suggestion (hint) to the compiler. If all CPU registers are occupied or the data type is too large to fit in a register, the compiler automatically stores the variable in standard RAM memory.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!