Storage Classes

Table of Contents

1. Storage Classes

1.1. Auto

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* Author: Marcos Azevedo (psylinux@gmail.com)
 * Date: 2019-12-13
 * Last Modified: 2019-12-17
 * Description: Using the storage classes auto */

#include<stdio.h>

void test2(){
    /* If we do not explicitly initialize the variable, this content can be garbage.
     * The lifetime is just within the function scope. */
    int k; /* The same as "auto int k" */

    printf("k = %d\n", k);
    k = 20;
    printf("k = %d\n", k);
}

void test1(){
    /* If we do not explicitly initialize the variable, this content can be garbage.
     * The lifetime is just within the function scope. */
    auto int var; /* The same as "int var" */

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


int main(){
    test2(); /* k can be anything since it is not initialized */
    test1(); /* var will get the remaining garbage on the stack  */

    return 0;
}

1.2. Static

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* Author: Marcos Azevedo (psylinux@gmail.com)
 * Date: 2019-12-13
 * Last Modified: 2019-12-17
 * Description: Using the storage classes static */

#include<stdio.h>

void test2(){
    /* The lifetime of this varible will be the same as the program
     * k is initialized with zero */
    static int k;

    printf("k = %d\n", k);
    k = 20;
    printf("k = %d\n", k);
}

void test1(){
    /* The lifetime of this varible will be the same as the program
     * var is initialized with zero */
    static int var;

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

int main(){
    test2();
    test1();

    return 0;
}

1.3. Extern

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Author: Marcos Azevedo (psylinux@gmail.com)
 * Date: 2019-12-13
 * Last Modified: 2019-12-17
 * Description: Using the storage classes extern */

#include<stdio.h>

/* The lifetime of a global variable will be the same as the program
 * gVar is initialized with zero */
int gVar;

void test2(){
    /* If we do not explicitly initialize the variable, this content can be
     * garbage. The lifetime is just within the function scope. */
    int k;

    printf("k = %d\n", k);
    k = 20;
    printf("k = %d\n", k);
    gVar = 10;
    printf("Inside test2: = %d\n", gVar);
}

void test1(){
    /* The lifetime of this variable will be the same as the program
     * var is initialized with zero */
    static int var;

    printf("var = %d\n", var);
    var++;
    printf("var = %d\n", var);
    gVar = 20;
    printf("Inside test1: gVar = %d\n", gVar);
}

int main(){
    printf("#Inside Main: gVar = %d\n", gVar);
    test2();
    test1();
    printf("#Inside Main: gVar = %d\n", gVar);

    return 0;
}

1.4. Register

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/* Author: Marcos Azevedo (psylinux@gmail.com)
 * Date: 2019-12-13
 * Last Modified: 2019-12-17
 * Description: Using the storage classes register */

#include<stdio.h>

int main(){

    register int i;
    printf("#Entering the Main: i = %d\n", i);

    for(i=0; i<10; i++){
        printf("#Inside the loop: i = %d\n", i);
    }
    printf("#Leaving the Main: i = %d\n", i);

    return 0;
}