Top 10 programs on C: Storage Classes

Ten C programs that demonstrate how to use various storage classes in C programming are provided below:

Auto Storage Class:

#include <stdio.h>

int main() {
    auto int a = 10;
    printf("Value of 'a': %d\n", a);
    return 0;
}

shows the ‘auto’ storage class, which is rarely used directly and is instead the default for local variables.

Static Storage Class:

#include <stdio.h>

void count() {
    static int num = 0;
    num++;
    printf("Value of 'num': %d\n", num);
}

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

exemplifies the’static’ storage class, which keeps a variable’s value constant across function calls.

Register Storage Class:

#include <stdio.h>

int main() {
    register int i;
    int sum = 0;
    for (i = 1; i <= 10; i++) {
        sum += i;
    }
    printf("Sum: %d\n", sum);
    return 0;
}

demonstrates how to utilize the ‘register’ storage class, which is used to recommend storing a variable in a register for quicker access.

Extern Storage Class:

#include <stdio.h>

extern int x; // Declaration of variable x defined in another file

int main() {
    printf("Value of 'x' from another file: %d\n", x);
    return 0;
}

shows the ‘extern’ storage class, which means that the variable is defined outside of the program, usually in a different file.

Combining Storage Classes:

#include <stdio.h>

extern int x;
static int y = 5;

int main() {
    auto int z = 10;
    printf("Value of 'x' from another file: %d\n", x);
    printf("Value of 'y': %d\n", y);
    printf("Value of 'z': %d\n", z);
    return 0;
}

demonstrates the use of many storage types (‘extern’,’static’, and ‘auto’) in one programme by combining them.

Thread Local Storage (TLS) Storage Class:

#include <stdio.h>
#include <threads.h>

_Thread_local int tls_var = 10;

int main() {
    printf("Value of TLS variable: %d\n", tls_var);
    return 0;
}

creates a variable with thread-local storage to demonstrate the _Thread_local storage class.

Mutable Storage Class:

#include <stdio.h>

int main() {
    mutable int x = 5;
    const int *ptr = &x;
    printf("Initial value: %d\n", *ptr);
    x = 10; // Mutable variable can be modified through a pointer to const
    printf("Modified value: %d\n", *ptr);
    return 0;
}

demonstrates the’mutable’ storage class, which permits changing a variable even when it is referenced by a ‘const’ pointer.

Automatic Storage Class with Functions:

#include <stdio.h>

void func() {
    auto int a = 20;
    printf("Value of 'a' inside function: %d\n", a);
}

int main() {
    func();
    // 'a' is local to func and cannot be accessed here
    // printf("Value of 'a' outside function: %d\n", a); // Uncommenting will cause an error
    return 0;
}

shows how a function’s ‘auto’ storage class works when a variable’s scope is restricted to that function.

Combining Storage Classes in Functions:

#include <stdio.h>

void func() {
    static int x = 0;
    printf("Value of 'x': %d\n", ++x);
}

int main() {
    auto int y = 5;
    func();
    func();
    printf("Value of 'y': %d\n", y);
    return 0;
}

merges the ‘auto’ and’static’ storage classes in functions, highlighting their distinct characteristics.

Global and Local Variables with Same Name:

#include <stdio.h>

int x = 10; // Global variable

int main() {
    int x = 20; // Local variable
    printf("Value of local 'x': %d\n", x); // Accessing local variable
    printf("Value of global 'x': %d\n", ::x); // Accessing global variable using :: operator
    return 0;
}

uses the scope resolution operator (::) to show how to distinguish between global and local variables with the same name.

These ten programs demonstrate the varied functions and implications of the numerous storage classes in C programming, further illuminating their subtle use and application in various contexts. It is essential to comprehend these storage classes in order to use variables and manage memory in C programs efficiently.

More Related Posts:

Top 10 programs on C: Data Types

Top 10 programs on C: Operators

Leave a Comment