DEV Community

Cover image for Storage Classes in C
supernerdd007
supernerdd007

Posted on

Storage Classes in C

Every variable possesses 2 features i.e. type and storage class. Type refers to the data type of the variable whereas storage class determines the visibility and scope of the variable.

Types of Storage Class in C

1. Automatic Storage class

Auto storage class is the default storage available in C. Objects having auto storage are defined by a system with a garbage value. It is for variables defined inside a function or a block often referred to as local variables.

{
    char rank;
    auto char rank;
}
Enter fullscreen mode Exit fullscreen mode

Both are the same way of declaring a variable having an automatic storage class.

2. Register Storage Class

Variables with register storage have properties similar to automatic storage classes in many aspects like local scope, garbage initial value.
However, instead of storage on RAM, variables with registered storage classes are stored on registers. Thus the maximum size of such variables would be restricted to the size of the register. Operators like unary '&' operator are not applied to it.
Examples of such variables are counter etc.

{
   register int counter_count;
}
Enter fullscreen mode Exit fullscreen mode

3. Static Storage Class

Variables having static storage are initialized during compile time. Thus they are independent of the function call stack, which makes them alive even after the function scope is over.
The life of the static variable remains till the end of the program. By default, static variables are initialized with the value ‘0’.

#include <stdio.h>

void func_count();

static int glob_var = 6; /* global variable with initial value of 6 */

main() {

   for (;  ; glob_var --) {
      func_count();
   }

   return 0;
}


void func_count() {

   static int y = 7; /* local static variable with initial value of 7*/
   y++;

   printf(" y is %d and glob_var is %d\n", y, glob_var);
}
Enter fullscreen mode Exit fullscreen mode

4. External Storage Class

Variables with extern storage classes are used to give reference to global variables which are visible to ALL program files.

In a program having multiple files, it becomes customary to define a global variable or function, which can be used in other files, then usage of extern becomes handy. It will be used in another file to provide a reference of a defined variable or function.

// file 1

# include <stdio.h>
int counter ;
extern void print_extern();

int main (){
    counter = 5 ;
    print_extern ();
}

//file 2
# include <stdio.h>
extern int counter ;

void print_extern(){
    printf ("counter is %d", counter);
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article we have looked into only the types of storage classes. If one needs to look further on memory layout in C and how storage takes place in C, then one should look into this curated article published by Scaler topics on Storage Classes in C.

Discussion (0)