C Programming • Structure and Union
C Programming / Types of storage classes

Types of storage classes

Notes 4 Structure and Union

A storage class in C specifies important properties of a variable such as its scope, lifetime, and how it is stored and accessed by the program.

Notes

Storage Classes

A storage class in C specifies important properties of a variable such as its scope, lifetime, and how it is stored and accessed by the program.

💡 Example

A variable declared inside a function is normally used only within that function, while a global variable can be accessed from multiple functions.

Types of Storage Classes

C provides four commonly studied storage classes: auto, register, static, and extern.

1. auto Storage Class

The auto storage class is the default storage class for local variables declared inside a function or block. The variable exists only while that function or block is executing.

💡 Example
auto int age = 20;

Here, age is a local variable with automatic storage duration. The keyword auto is normally omitted because local variables are automatic by default.

2. register Storage Class

The register storage class requests that a variable be kept in a CPU register for faster access when possible. It is generally used for frequently accessed local variables.

💡 Example
register int count = 0;

Here, count is declared as a register variable. The compiler decides whether it can actually place the variable in a CPU register.

3. static Storage Class

The static storage class allows a local variable to retain its value between function calls. A static local variable is initialized only once and continues to exist for the entire execution of the program.

💡 Example
static int count = 0; count++;

Each time the function is called, count keeps its previous value instead of starting again from zero.

4. extern Storage Class

The extern storage class is used to declare a variable that is defined elsewhere, usually as a global variable. It allows different parts of a program to refer to the same variable.

💡 Example
extern int marks;

Here, marks is declared as an external variable. Its actual definition is provided elsewhere in the program.

Storage Classes at a Glance

Storage Class Main Idea
auto Default local variable.
register Requests faster access through a CPU register.
static Retains value between function calls.
extern Refers to a variable defined elsewhere.

Scope and Lifetime

Scope tells where a variable can be accessed, while lifetime tells how long the variable exists during program execution.

💡 Example

A local auto variable is available only inside its block, while a static local variable continues to retain its value between calls to the function.

Practical Example

Problem Statement

Write a C program to demonstrate the use of a static variable by calling the same function multiple times and observing that the variable retains its previous value.

Learning Outcomes

  • Understand the purpose of the static storage class.
  • Observe how a static variable retains its value.
  • Understand the difference between repeated function calls and a newly created local variable.

Hint

Declare a local variable using static, increase its value on every function call, and call the function three times.

Theory

A static local variable is initialized only once and retains its value between function calls. Its scope remains local to the function, but its lifetime extends throughout the execution of the program.

Program

#include <stdio.h> void counter() { static int count = 0; // increase the retained value on each function call count++; printf("Count = %d ", count); } int main() { // call the same function three times counter(); counter(); counter(); return 0; }

Expected Output

Count = 1

Count = 2

Count = 3

Note

If count were an ordinary local variable instead of static, it would be initialized again whenever the function is called. The static variable retains its previous value.

Quick Revision

Storage Class Remember
auto Default for local variables.
register Requests fast access through a CPU register.
static Retains value between function calls.
extern Refers to a variable defined elsewhere.

Important Exam Questions

Short Answer Questions

  1. What is a storage class in C?
  2. Name the four storage classes in C.
  3. What is the auto storage class?
  4. What is the purpose of the register storage class?
  5. What is the use of the static storage class?
  6. What is the extern storage class?
  7. What is the difference between scope and lifetime?

Long Answer Questions

  1. Explain the different storage classes in C with suitable examples.
  2. Explain the static storage class with a suitable program.
  3. Differentiate between auto, register, static and extern storage classes.
🎥 Recommended Learning

Watch a beginner-friendly explanation of storage classes in C programming.

▶ Watch: Storage Classes in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for storage classes will be provided here.

🧠 Mind Map

Use the mind map for quick revision of auto, register, static and extern.