Skip to content

Storage Class in C

In C programming, storage classes are keywords that define the scope, visibility, and lifetime of variables within a program. They control where and how a variable is stored in memory and how it behaves during program execution. C provides four main storage classes: auto, register, static, and extern, along with the typedef keyword for defining custom data types. Let’s discuss each storage class in detail:

1. auto Storage Class:

  • Default Storage Class: Variables declared inside a function block (local variables) are by default of auto storage class.
  • Scope: Limited to the block in which they are defined.
  • Lifetime: Exists only as long as the block is executing.
  • Memory Allocation: Allocated and deallocated automatically on the function call stack.
  • Example:

#include <stdio.h>

int main() {

    auto int num = 10;

    printf(“Value of num: %d\n”, num);

    return 0;

}

2. register Storage Class:

  • Hint to Compiler: Suggests the compiler to store the variable in a CPU register for faster access.
  • Scope: Limited to the block in which they are defined.
  • Lifetime: Exists only as long as the block is executing.
  • Memory Allocation: Allocated and deallocated automatically on the function call stack.
  • Example:

#include <stdio.h>

int main() {

    register int count = 0;

    while (count < 10) {

        printf(“%d “, count);

        count++;

    }

    printf(“\n”);

    return 0;

}

3. static Storage Class:

  • Lifetime: Extends throughout the program execution.
  • Visibility:
    • File Scope: Variables declared outside any function are accessible only within the file where they are declared.
    • Function Scope: Variables declared inside a function maintain their values between function calls.
  • Memory Allocation:
    • Static Local Variables: Allocated in the data segment of memory.
    • Static Global Variables: Allocated in the data segment of memory.
    • Example:

#include <stdio.h>

void increment() {

    static int count = 0;

    count++;

    printf(“Count: %d\n”, count);

}

int main() {

    increment(); // Output: Count: 1

    increment(); // Output: Count: 2

    increment(); // Output: Count: 3

    return 0;

}

4. extern Storage Class:

  • Usage: Used to declare variables and functions that are defined in another file.
  • Visibility: Makes a variable or function accessible to multiple source files.
  • Declaration: Variables are declared in one file using extern and defined in another file without extern.
  • Example:

File1.c:

int num; // Declaration

File2.c:

#include <stdio.h>

extern int num; // Declaration

int main() {

    num = 10; // Definition

    printf(“Value of num: %d\n”, num);

    return 0;

}

5. typedef Storage Class:

  • Purpose: Defines custom data types using aliases.
  • Syntax: typedef existing_data_type new_data_type;
  • Example:

#include <stdio.h>

typedef int Integer;

int main() {

    Integer num = 10;

    printf(“Value of num: %d\n”, num);

    return 0;

}

Understanding storage classes is crucial for managing memory efficiently and controlling variable visibility and scope within C programs. Each storage class serves different purposes and has its own rules regarding memory allocation and variable behavior.