logo of SA Coder
        Menu
sign in

      References



Storage Class



When writing code in the C programming language, it's important to understand how variables are stored in memory or in the CPU registers, and how their lifecycle is managed. This is where storage classes come in.



In C, there are four types of storage classes: automatic, register, static, and external. Let's take a closer look at each one.


1. Automatic storage class



The automatic storage class is the default storage class in C programming language. Variables with this storage class are stored in memory, and their initial value is garbage. They have a local scope, which means they are only accessible within the block in which they are defined. Their lifecycle is limited to the duration of that block. Here's an example of how you can use automatic storage class:


copy

#include <stdio.h>

int main()
{
    auto int n = 15;
    printf("%d", n); 
    return 0;
}


2. Register storage class



The register storage class is similar to automatic storage class, except that the variable is stored in a CPU register instead of memory. This can result in faster access times for the variable, but the number of registers available for use is limited. Therefore, the compiler may not always be able to store the variable in a register. Variables with this storage class also have a local scope and limited lifecycle. Here's an example of how you can use register storage class:


copy

#include <stdio.h>

int main()
{
    register int n = 15;
    printf("%d", n); 
    return 0;
}


3. Static storage class



Variables with static storage class are stored in memory, and their initial value is zero. They have a local scope, but their lifecycle persists between different function calls. This means that their value is retained even after the function call has ended, and they can be accessed again the next time the function is called.



Here's an example of how you can use static storage class:


copy

#include <stdio.h>

void increment_with_static(){
    static int n = 1;
    printf("%d\n",n);
    n = n + 1;
}

int main()
{
    increment_with_static();
    increment_with_static();
    increment_with_static();
    return 0;
}


4. External storage class



Variables with external storage class are stored in memory, and their initial value is zero. They have a global scope, which means they can be accessed from anywhere in the program. Their lifecycle persists as long as the program's execution doesn't come to an end.



Here's an example of how you can use external storage class:


copy

#include <stdio.h>

extern int num;
int num = 15;

void print_globle()
{
    printf("%d\n", num);
    num = 20;
}

int main()
{
    print_globle();
    printf("%d\n", num);
    return 0;
}



In conclusion, understanding storage classes in C programming language is crucial for managing variables effectively. By knowing the different types of storage classes and their characteristics, you can write more efficient and effective code.




Please login first to comment.