D A R K - C O D E R

Loading

C Programming – Storage Classes

Storage class defines the scope and lifetime of a variable or methods(functions) within a C program. Scope determines the variable’s value accessible in which portion of the program. And lifetime determines how long variables or methods will last long.

In C programming, each variable has a storage class that describes its visibility and lifetime.

Scope: declare the scope or boundary of the variable or method. That determines the availability and accessibility of variables and functions (methods). Where to access it? When to access. How to access it? 

Default Value: i.e. if we do not initialize that variable, what will be its initial default value?

Lifetime: determine how long the variable exists.

in C programming four types of storage classes in a C program −

  • auto
  • register
  • static
  • extern

auto Storage Class

The auto-storage class is the default storage class for local variables. A local variable is a memory space or a variable defined inside a function and is only accessible within that function. The auto-storage class specifies that a local variable has automatic storage duration, which means that it is created when the process is called and it is destroyed when the function returns.

Here is an example of a local variable with automatic storage duration:

#include  

void print_message(void)

{

    auto int x = 10; // x is a local variable with automatic storage duration

    printf("The value of x is %d\n", x);

}

int main(void)

{

    print_message(); // x is created and initialized to 10

    print_message(); // x is created and initialized to 10 again

    return 0;

}

In this example, the variable x is defined inside the print_message function and has automatic storage duration. When the print_message process is called, x is created and initialized to 10. When the function returns, x is destroyed. If the function is called again, x is created and initialized again.

The register storage class specifies that a variable is stored in a register instead of in memory. Registers are faster than memory, so using the register storage class can improve the performance of your program. However, a limited number of registers are available on a given system, so you should only use the register storage class for variables accessed frequently in your program.

Here is an example of a local variable with register storage duration:

#include  

void print_sum(int a, int b)

{

    register int sum = a + b; // sum is a local variable with register storage duration

    printf("The sum of %d and %d is %d\n", a, b, sum);

}

int main(void)

{

    print_sum(1, 2); // sum is stored in a register

    print_sum(3, 4); // sum is stored in a register

    return 0;

}

In this example, the variable sum is defined inside the print_sum function and has register storage duration. When the print_sum function is called, the sum is stored in a register and used to hold the sum of a and b. When the function returns, the sum is no longer accessible.

The static storage class specifies that a variable or function has static storage duration, which means that it is created when the program starts and persists until the program ends. This contrasts local variables, which are created and destroyed every time the function is called.

Here is an example of a local variable with static storage duration:

#include  

void print_count(void)

{

    static int count = 0; // count is a local variable with static storage duration

    printf("count = %d\n", ++count);

}

int main(void)

{

    print_count(); // count is initialized to 0

    print_count(); // count is incremented to 1

    print_count(); // count is incremented to 2

    return 0;

}
#include  

void print_count(void)

{

    static int count = 0; // count is a local variable with static storage duration

    printf("count = %d\n", ++count);

}

int main(void)

{

    print_count(); // count is initialized to 0

    print_count(); // count is incremented to 1

    print_count(); // count is incremented to 2

    return 0;

}

In this example, the variable count is defined inside the print_count function and has a static storage duration. When the print_count function is called, the count is initialized to 0 and incremented by one every time the function is called. Because count has a static storage duration, it persists between function calls and retains its value.

Here is another example of a function with static storage duration:

#include  

int add(int x, int y) // add is a function with static storage duration

{

    return x + y;

}

int main(void)

{

    printf("The sum of 1 and 2 is %d\n", add(1, 2)); // add is called

    printf("The sum of 3 and 4 is %d\n", add(3, 4)); // add is called again

    return 0;

}

In this example, the function add has a static storage duration and can be called multiple times throughout the program.

The extern storage class specifies that a variable or function is defined in another source file and provides a reference to that entity in the current source file. The extern storage class is used to declare global variables and functions defined in another source file and needs to be accessed in the existing source file.

Here is an example of using the extern storage class to access a global variable defined in another source file:

// file1.c
#include   

Int x; // x is a global variable with external linkage.

void print_x(void)

{

    printf("x = %d\n", x);

}
// file2.c
#include  

extern int x; // declare x as a global variable with external linkage

int main(void)

{

    x = 10;

    print_x(); // prints "x = 10"

    return 0;

}

In this example, the global variable x is defined in file1.c and has external linkage. It can be accessed in file2.c by declaring it with the extern storage class when the print_x function is called in file2.c, it prints the value of x, which has been set to 10 in file2.c.

Here is another example of using the extern storage class:

// File: main.c

#include  
 
extern int count; // count is defined in another source file

int main(void)

{

    printf("Count: %d\n", count); // count is accessed from another source file

    return 0;

}
// File: count.c

#include  

Int count = 0; // count is defined in this source file

void increment_count(void)

{

    ++count;

}

In this example, the variable count is defined in the count.c source file and can be accessed from the main.c source file using the extern storage class. The increment_count function is also defined in the count.c source file and can be called from the main.c source file.