Storage Classes in C

In this tutorial, you will learn the basics of storage classes in C. You will understand their types, purpose, and how they affect the scope of variables. We will also look at how storage classes change the lifetime and visibility of variables in a program. You will explore the roles of auto, register, static, and extern. Real-world examples will help you understand these concepts better.

Contents:

  1. What are Storage Classes in C?
  2. Types of Storage Classes in C
  3. auto Storage Class in C
  4. register Storage Class in C
  5. static Storage Class in C
  6. extern Storage Class in C
  7. Difference Between Storage Class and Data Type in C
  8. Scope, Lifetime, and Linkage in C
  9. FAQs on Storage Classes in C

What are Storage Classes in C?

In C programming, storage classes define the scope (visibility), lifetime, and linkage of variables and functions. They tell the compiler how long a variable should exist in memory, where it can be accessed, and whether it can be accessed from other files.

Types of Storage Classes in C

There are four storage classes in C:

  • auto Storage Class
  • register Storage Class
  • static Storage Class
  • extern Storage Class

advertisement

auto Storage Class in C

The auto storage class is the default storage class for all local variables inside a function or block in C. So even if you don’t write auto, the variable is treated as auto by default.

Key Features of auto:

  • Scope: Local to the block or function where it is defined.
  • Lifetime: Exists only while the block/function is running. Memory is freed after execution ends.
  • Default storage class for local variables.
  • Not necessary to write auto explicitly, but you can.

Syntax:

auto data_type variable_name;

Although, rarely use it explicitly since it’s the default for local variables.

Free 30-Day Python Certification Bootcamp is Live. Join Now!

Example:

#include <stdio.h>
 
void showMarks()
{
    auto int marks = 80; // explicitly using 'auto'
    printf("Student Marks: %d\n", marks);
}
 
int main() {
    showMarks();
    return 0;
}

Output:

Student Marks: 80

This program shows how the auto storage class works in C. The marks variable is declared inside a function using auto, which is the default for local variables. It stores the value 80 and prints it. The variable exists only while the function runs.

register Storage Class in C

The register storage class is used to store variables in CPU registers instead of RAM to make access faster. It’s a hint to the compiler to optimize performance for variables used very frequently, like loop counters.

Key Features of register:

  • Scope: Local to the block/function.
  • Lifetime: Temporary (as long as the block/function runs).
  • Faster access (intended for performance-critical variables).
  • Cannot get the address using the & operator.

Syntax:

advertisement
register int counter;

Note: You can’t get the address of a register variable using & operator.

Example:

#include <stdio.h>
 
int main()
{
    register int i;
    for (i = 1; i < 5; i++) {
        printf("Sanfoundry Quiz %d\n", i);
    }
    return 0;
}

Output:

Sanfoundry Quiz 1
Sanfoundry Quiz 2
Sanfoundry Quiz 3
Sanfoundry Quiz 4

This program shows how the register storage class works in C. It stores the variable i in a CPU register (if possible) to speed up access. The program runs a loop from 1 to 4 and prints a message with the current count each time. The register keyword tells the compiler that the variable will be used often.

static Storage Class in C

The static storage class in C is used to retain the value of a variable across multiple function calls. It extends the lifetime of a variable to the entire program, even though the variable might be local in scope.

Syntax:

static int counter = 0;

Example:

#include <stdio.h>
 
void showCounter() {
    static int count = 0;  // retains its value across calls
    count++;
    printf("Sanfoundry Call Count: %d\n", count);
}
 
int main() {
    showCounter();
    showCounter();
    showCounter();
    return 0;
}

Output:

Sanfoundry Call Count: 1
Sanfoundry Call Count: 2
Sanfoundry Call Count: 3

This program uses the static storage class in C. It declares a variable count that keeps its value between function calls. Each time the program calls showCounter(), it increases count by 1 and prints it. The program calls the function three times, so it prints an increasing count from 1 to 3.

extern Storage Class in C

The extern storage class in C is used to declare a global variable or function that is defined in another file or at a later point in the same file. It allows multiple files in a program to share the same global variable or function.

Syntax:

extern data_type variable_name;

file1.c:

int sanfoundryMarks = 95;

file2.c:

#include <stdio.h>
 
extern int sanfoundryMarks;  // Declared from file1.c
 
int main() {
    printf("Certification Marks: %d\n", sanfoundryMarks);
    return 0;
}

Compile together:

gcc file1.c file2.c -o program
./program

Output:

Certification Marks: 95

This program shows how the extern storage class works in C. The variable sanfoundryMarks is defined in file1.c and shared with file2.c using the extern keyword. The main() function prints the value of that variable. This lets one file access a variable defined in another.

Difference Between Storage Class and Data Type in C

Here are the key difference between Storage Class and Data Type in C

Feature Storage Class Data Type
Purpose Determines lifetime, scope, visibility, and memory location of a variable Defines type of data a variable holds (e.g., int, float)
Examples auto, register, static, extern int, float, char, double, long, etc.
Focus Area Focuses on how and where the variable is stored and used Focuses on what kind of data the variable stores
Memory Allocation Affects how memory is allocated and managed Affects how much memory is allocated per type
Initialization Controls default values and initialization behavior Does not handle initialization rules
Scope & Lifetime Controls scope (visibility) and lifetime (how long variable exists) Data type has no control over scope or lifetime
Can Be Combined? Yes, you can combine with data types Yes, e.g., static int x = 5;

Scope, Lifetime, and Linkage in C

These three terms define how variables behave in a C program in terms of visibility, existence, and accessibility.

1. Scope – Where a variable is accessible

The Scope refers to the region of the program where the variable is visible and can be used.

Scope Type Description Example
Block Scope Variable declared inside {} — accessible only within that block int main() { int x = 5; }
Function Scope Labels (like goto labels) have function scope goto label; … label:
File Scope Variable declared outside all functions — accessible throughout the file int count = 0;
Function Prototype Scope Variables declared in function prototype int add(int a, int b);

2. Lifetime – How long a variable exists in memory

Lifetime refers to the duration for which the variable retains its value during program execution.

Storage Class Lifetime Memory Retention
auto Created and destroyed in block No persistence
static Exists for the entire program Persists values
register Same as auto (faster access) No persistence
extern Exists as long as program runs Global memory

3. Linkage – Whether variable can be used in other files

Linkage determines whether multiple declarations refer to the same entity (variable or function) across multiple files.

Type of Linkage Description Keyword Used
Internal Linkage Variable/function accessible only within the current file static
External Linkage Accessible across different files default or extern
No Linkage Each declaration is a separate entity auto, register

Example:

#include <stdio.h>
 
int globalVar = 10; // File Scope, External Linkage, Static Lifetime
 
void demo() {
    static int count = 0;  // Block Scope, Internal Linkage, Static Lifetime
    int temp = 5;          // Block Scope, No Linkage, Auto Lifetime
 
    count++;
    printf("Count: %d, Temp: %d, Global: %d\n", count, temp, globalVar);
}
 
int main() {
    demo();
    demo();
    return 0;
}

Output:

Count: 1, Temp: 5, Global: 10
Count: 2, Temp: 5, Global: 10

This program shows how different storage classes work in C. The global variable globalVar keeps its value and is shared across the file. The count variable inside the function uses static, so it keeps its value between calls. The temp variable is local and resets each time. When demo() runs twice, count increases, but temp stays the same.

FAQs on Storage Classes in C

1. What are storage classes in C?
Storage classes in C define the scope (visibility), lifetime, and linkage of variables or functions. They help the compiler understand how to handle the variable in terms of memory allocation and accessibility.

2. How many storage classes are there in C?
There are four main storage classes in C: auto, register, static and extern.

3. What is the default storage class of a local variable?
The default storage class of a local variable is auto. However, you usually don’t need to specify it explicitly, as it’s assumed automatically.

4. What is the use of the register storage class?
The register storage class suggests that the variable be stored in the CPU register for faster access. It cannot be used with global variables or with the address-of (&) operator.

5. What is a static variable in C?
A static variable retains its value between function calls. It has internal linkage and a lifetime throughout the program, even if it is declared inside a function.

6. What is the purpose of the extern keyword?
The extern keyword is used to declare a global variable or function that is defined in another file or later in the code. It tells the compiler that the variable exists, but the memory is allocated elsewhere.

7. Can we use static with global variables?
Yes, a global variable declared with static has file scope, meaning it is accessible only within the file where it’s declared.

Key Points to Remember

Here is the list of key points we need to remember about “Storage Classes in C”.

  • Storage classes control a variable’s scope, lifetime, and linkage in a C program.
  • The four storage classes in C are: auto, register, static, and extern.
  • auto is the default storage class for local variables inside functions or blocks.
  • register suggests storing a variable in a CPU register for faster access but doesn’t allow using the & operator.
  • static keeps the value of a variable between multiple function calls.
  • extern allows variables to be shared across multiple files in a program.
  • Storage classes are different from data types; they manage how and where a variable is stored, not what data it holds.
  • Scope defines visibility, lifetime defines duration in memory, and linkage defines accessibility across files.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.