C Storage Class

In C programming language, storage class specifies the lifetime, scope, and visibility of variables and functions. There are four storage classes in C: auto, extern, static, and register. Understanding these storage classes is essential for efficient memory usage and optimization of C programs.

In C programming language, there are four types of storage class. They are:

  1. Auto: Variables declared as “auto” have a local scope and are allocated memory automatically when the function is called. The memory is released when the function returns.
  2. Extern: Variables declared as “extern” have a global scope and can be accessed from any function in the program. They are defined in a separate file and are linked with the program at compile time.
  3. Static: Variables declared as “static” have a local scope and retain their value between function calls. They are allocated memory once and hold the value until the program terminates.
  4. Register: Variables declared as “register” are stored in the CPU registers instead of memory for faster access. They have a local scope and are useful for frequently accessed variables within a function.

Here’s an example of using these storage classes in C:

#include <stdio.h>

void func() {
    auto int a = 10; // local scope, automatically allocated memory
    extern int b; // global scope, defined in another file
    static int c = 0; // local scope, retains value between function calls
    register int d = 20; // local scope, stored in CPU registers for faster access
    printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);
    c++; // increment static variable

int main() {
    extern int b; // global variable declared in another file
    b = 30; // set global variable value
    func(); // call function
    func(); // call function again
    return 0;

int b; // define global variable

In this example, the “func” function declares and initializes four variables using different storage classes. The “main” function declares the global variable “b” and sets its value to 30. The “func” function is called twice, and each time it prints the values of the four variables, including the incremented value of the static variable “c”. Finally, the program terminates by returning 0.

Understanding and properly using storage classes in C programming language can help improve program performance and optimize memory usage.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial