C Programming language-Storage Classes in C

Storage Classes in C

The storage classes define the scope of the variables and their functions within a C program. We have four different storage classes in C such as:
  • auto
  • register
  • static
  • extern
auto – Storage Class

The auto storage class is the default storage class for all the variables.

Example:

{
int days;
auto int months;
}

From the above example, we define two variables inside the same storage class. The storage class ‘auto’ can only be used within functions i.e., local variables.

register -  Storage Class

The register storage class is used to define local variables which is stored in the register instead of RAM. This means that the variable has the maximum size equal to the register size which is usually one word and it can’t have unary ‘&’ operator applied to it. The register storage class does not have any memory location.

Example:

{
register int year;
}
The register mostly used for  variables which require quick access such as counters. 

static –Storage Class

The static storage class tells the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time when it gets executed each time it comes into and goes out of the scope. Making the local variable static allows them to maintain their values between function calls.

The static modifier can also be applied to global variables. When the static is used on a global variable, it causes only one copy of that member and it shared by all the objects of its class.

Example:

#include 

/* Function Declaration */
void func(void);

static int count = 10; /* Global static variable */

main() {

   while(count--) {
      func();
   }

   return 0;
}

/* function definition */
void func( void ) {

   static int number = 10; /* local static variable */
   number++;

   printf("number is %d and count is %d\n", number, count);
}

Output:

number is 11 and count is 9
number is 12 and count is 8
number is 13 and count is 7
number is 14 and count is 6
number is 15 and count is 5
number is 16 and count is 4
number is 17 and count is 3
number is 18 and count is 2
number is 19 and count is 1
number is 20 and count is 0
extern – Storage Class

The extern storage classes  are most commonly used when there are two or more files sharing the same global variables  or functions. This ‘extenr’ storage class is used to give a reference of a global variable which is visible to all the program files. The ‘extern’ variables cannot be initialized as it points to the variable name at a storage location that has been previously defined.
In simple terms, ‘extern’ is used to declare a global variable or function in another file.

File 1: file1.c

#include 

int count ;
extern void func();

main() {
   count = 10;
   write_extern();
}
File 2: file2.c

#include 

extern int count;

void func(void) {
   printf("count is %d\n", count);
}

Output:

count is 10
In the above example, the extern is being used to declare the variable count in the second file file2.c and its definition is in the first file file1.c. When you compile both the files, you get the above output.