Storage Classes in C Programming

A storage class defines the scope (visibility) and lifetime of variables in a C Program. There are four different storage classes in C programming.

1. auto

The auto storage class is the default storage class for all local variables in C programming. Either a variable is defined as auto storage class or not, it will still be auto by default. Auto can only be used inside a function.

Example :

int a;
auto int a;

Here, both variables are of auto storage class.

2. register

The register storage class is used to define local variables that should be stored in a register instead of RAM depending on hardware and implementation restrictions. Such variables have a maximum size equal to the register size (usually one word) and can't have the unary '&' operator included in their names to it as unary operator does not have a memory location. The register should only be used for variables that require quick access.

Example :

register int a;

3. static

The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time the program runs maintaining its value between function calls.

The static storage class can also be applied to global variables. It restricts the variable's scope within the file in which it is declared. Only a copy of such variable is shared by all the objects of its class.

Example (local variable of static storage class): 

#include <stdio.h>
 
static int a = 1; /* global variable */

main() {
   while(a++) { /* increase the value of a each time the program runs */
     if(a <= 6){ /* execute function serealize until the value of a is less than or equals to 6 */
       serealize();
     }else{
        return 0; /* close the program once the value of a is greater than 6 */
     }
   }
   return 0;
}

serealize() {
   static int i = 0; /* local static variable */
   i++; /* increase the value of i each time this function runs */
   printf("%d\n", i);
}

Output

1
2
3
4
5

Example (Local variable of auto storage class);

#include <stdio.h>
 
static int a = 1; /* global variable */

main() {
   while(a++) { /* increase the value of a each time the program runs */
     if(a <= 6){ /* execute function serealize until the value of a is less than or equals to 6 */
       serealize();
     }else{
        return 0; /* close the program once the value of a is greater than 6 */
     }
   }
   return 0;
}

serealize() {
   int i = 0; /* local auto variable */
   i++; /* increase the value of i each time this function runs */
   printf("%d\n", i);
}

Output

1
1
1
1
1

4. extern

The extern storage class is used to declare a storage location that has already been defined in another C program file. The extern storage class is used when there are two or more files sharing the same global variables or functions.

Example :

First File (file1.c):

#include <stdio.h>
 
int a;
main() {
   a = 10;
   print_data();
}

Second File (file2.c):

#include "file1.c"
#include <stdio.h>
extern int a;

print_data() {
   printf("Value of extern a is %d\n", a);
   return 0;
}

Output

Value of extern a is 10


0 Like 0 Dislike 0 Comment Share

Leave a comment