Understanding the Different Types of Storage Classes in C

·

Introduction

Storage classes in C determine the scope, lifetime, visibility, and memory location of variables. Understanding these is crucial for optimizing your C programs…

Types of Storage Classes

  • auto: The default storage class for local variables.
  • register: Suggests storing the variable in a CPU register for faster access.
  • static: Maintains the variable’s value between function calls.
  • extern: Declares a global variable accessible across multiple files.

Examples

Here’s a practical example of a static variable:

void count() {
    static int counter = 0;
    counter++;
    printf("Counter: %dn", counter);
}

Calling count() multiple times will show the value of counter persisting across calls.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *