The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Storage Classes in C

We have seen that every variable has a data type. To fully define a variable, we need a storage class as well apart from data type. But you might be thinking that we haven't used any storage class so far. Actually, we were using a default storage class till now. When we do not define a storage class, compiler automatically assigns a default storage class to it. A variable's storage class gives information about the storage location of variable in memory, default initial value, scope of the variable and it's lifetime.
Storage class specifiers supported in C are auto, register, static and extern.

' auto ' storage class
This is the default storage class we have been using so far. It applies to local variables only and the variable is visible only inside the function in which it is declared and it dies as soon as the function execution is over. If not initilaized, variables of class auto contains garbage value.
Following example shows how variables of auto storage class are declared :

int var; /* auto is the default storage class */
auto int var;

' register ' storage class
Variables of class 'register' are stored in CPU registers instead of memory which allows faster access. The scope of the variable is local to the function in which it is defined and it dies as soon as the function execution is over. It contains some garbage value if not initialized. It is declared as :

register int var;

' static ' storage class
The scope of static variable is local to the function in which it is defined but it doesn't die when the function execution is over. The value of a static variable persists between function calls. The default initial value of static variable is 0. Following program illustrates the behaviour of static variables :

#include <stdio.h>

void func() {
   static int count = 0;
   /* value of 'count' persist between function calls
    * i.e count doesn't become 0 every time func() is called;
    * previous value of count remains alive
    */
   count++;
   printf("Function is called %d times\n", count);
}

int main() {
   printf("Calling Function :- \n");
   func();
   printf("Calling Function :- \n");
   func();
   printf("Calling Function :- \n");
   func();
   return 0;
}


' extern ' storage class
Variables of extern storage class have a global scope. We use extern variables when we want a variable to be visible outside the file in which it is declared. So, an extern variable can be shared across multiple files. An extern variable remains alive as long as program execution continues. A static global variable is visible only in the file it is declared but an extern global variable is visible across all the files of a program.
We have used declaring and defining a variable interchangeably but extern storage class adds a distinction.
In simple words :

extern int count; /* declaration of variable 'count' */
int count; /* defination of variable 'count' */

The declaration and definition of a variable can be in different files.
Following program shows the use of extern storage class :

/*
 * File : extern_var.c
 */

#include <stdio.h>
#include "extern_var_util.c" /* including the contents of a user defined file */

extern int count; /* declaration of variable 'count' */

int main() {
    printf("count : %d\n", count);
    count = 3;
    incrementCount();
    printf("count : %d\n", count);
    return 0;
}
/*
 * File : extern_var_util.c
 */

int count = 9; /* defination of variable count */

void incrementCount() {
   ++count;
}

We can observe that the variable count is declared in one file and it is defined in other file. It can be accessed and modified in any of the files and the updated value of count is reflected in both the files.

Back | Next