Day 12 - Storage Classes

Day 12 - Storage Classes

dev.to - Sep 24

Day 12 of the ? Days of Code is finally over, and today I learnt about variable and function scope, a crucial aspect of inheritance.

Introduction

A storage class is used to set the scope of a variable or function. By knowing the storage class of a variable, we can determine the life-time of that variable during the run-time of the program.

There are four different storage-class specifications in C: automatic, external, static and register. They are identified by the keywords auto, extern, static, and register, respectively.


auto

Automatic variables are always declared within a function and are local to the function in which they are declared; that is, their scope is confined to that function. Automatic variables defined in different functions will therefore be independent of one another, even though they may have the same name.

Example:

#include <stdio.h>

int foo() {
    auto int i = 10;
    return i;
}

int bar() {
    auto int i = 20;
    return i;
}

void main() {
    int a = foo();
    int b = bar();
    printf("%d,%d",a,b); // 10, 20
}
Enter fullscreen mode Exit fullscreen mode

extern

External variables, in contrast to automatic variables, are not confined to single functions. Their scope extends from the point of definition through the remainder of the program. Hence, they usually span two or more functions, and often an entire program. They are often referred to as global variables.

Since external variables are recognized globally, they can be accessed from any function that falls within their scope. They retain their assigned values within this scope. Therefore an external variable can be assigned a value within one function, and this value can be used (by accessing the external variable) within another function.

Example:

File 1 - index.h

int a = 10;
int b = 20;
Enter fullscreen mode Exit fullscreen mode

File 2 - main.c

#include <stdio.h>
#include "index.h"

void main() {
    extern int a,b;
    int sum = a + b;
    printf("%d + %d = %d",a,b,sum); // 10 + 20 = 30
}
Enter fullscreen mode Exit fullscreen mode

static

In a single-file program, static variables are defined within individual functions and therefore have the
same scope as automatic variables; i.e., they are local to the functions in which they are defined. Unlike
automatic variables, however, static variables retain their values throughout the life of the program. Thus, if a
function is exited and then re-entered at a later time, the static variables defined within that function will retain
their former values. This feature allows functions to retain information permanently throughout the execution
of a program.

Example:

#include <stdio.h>

int foo() {
    static int i = 0;
    int j = 0;

    i += 10;
    j += 10;
    printf("i = %d, j = %d\n",i,j);
}

void main() {
    for (int i = 0; i < 5; ++i) {
        foo();
    };
}
Enter fullscreen mode Exit fullscreen mode

Output -

i = 10, j = 10
i = 20, j = 10
i = 30, j = 10
i = 40, j = 10
i = 50, j = 10
Enter fullscreen mode Exit fullscreen mode

register

The register storage class specifier indicates to the compiler that the object should be stored in a machine register. The register storage class specifier is typically specified for heavily used variables, such as a loop control variable, in the hopes of enhancing performance by minimizing access time. However, the compiler is not required to honor this request. Because of the limited size and number of registers available on most systems, few variables can actually be put in registers. If the compiler does not allocate a machine register for a register object, the object is treated as having the storage class specifier auto.

Example:

#include <stdio.h>

void main() {
    register int i;
    printf("Memory Address = %p",&i);
}
Enter fullscreen mode Exit fullscreen mode

This throws a error

error: address of register variable 'i' requested
Enter fullscreen mode Exit fullscreen mode

as the variable is stored in machine register instead of RAM.


There are two more storage classes named typedef and _Thread_local which will be done later.


MORE ARTICLES