Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

Storage Classes in C language

In the previous tutorials, we have discussed about the conditional statements. In this tutorial we'll be learning about something known as Storage Classes in the C language. Every variable you define in the C language is generally associated with a Storage class which basically defines the Scope and lifetime of that variable.

Before we get into detail about this, let's first understandwhat is exactly meant by scope and lifetime of variable.

Now, let us take a look at different types of storage classes available in C programming language:

  • auto
  • register
  • static
  • extern

Storage classes in C language and keywords used to define them. auto, extern, static and register.

Now, we'll understand each one of this in detail :

Auto (Automatic) :

The default storage class of any variable is Automatic . It is created as soon as the declaration statement is encountered and is destroyed as soon as the program control leaves the block which contains your variable.

*Keyword used - auto*

For example, here we define two variables, first, count, which is a local variable and second,  counter inside a block (local variable using auto class).

  int count;
  auto int counter;

External Storage Class (Extern) :

They are the Global Variables, i.e. declared outside the function block but are accessible inside the function block as well. They are accessible by all the function blocks present in the program and hence they have lifetime for entire program.

It is actually used to give a reference to the global variable which can be accessed in all your files. If we need to have a global variable, then in one file, we use that particular variable or function, and in the other file we define it by keyword extern so that when it is used in our other file, the reference is used from the first file.

Keyword used - extern

For example, let's have two files, first which would have our main function (main.c) and second one having a function named extern_storage (extern.c)

First file : main.c

int counter = 1 ; // Global Variable

void main()

Second file : extern.c

void extern_storage(void);

extern int counter;

void extern_storage(void)
   printf("Current value of extern counter is %d n", &counter);

When the above two files are executed, the function extern_storage picks up the current value from our main.c file. So, the following output will be produced.

Current value of extern counter is 1

Current value of extern counter is 2

Current value of extern counter is 1

Static Storage Class (static) :

It has visibility of a local variable but lifetime of external variable. Thus, once defined, it would not be destroyed after the function finishes execution but would retain it's value in memory and can be used further in future function calls.


Keyword used - static


(Self-Explanatory through post on Scope and lifetime of variables)

Register Storage Class (register) :

It is very similar in behavior to an automatic storage class variable. The difference between the two comes from the fact that auto storage class uses main memory for storage but register storage class variables might use CPU Registers.

NOTE: register keyword doesn't guarantee that CPU Registers would be used every time, it depends on hardware and other restrictions that may be there.

The use of these registers facilitates faster accessing and helps in running programs faster. Thus variables that need quick access  like variables for storing counter values should be declared by using register keyword.

For example :

  register int count;

We hope the concept of storage classes is now clear ! Have any doubt or something to say? Let us know through comments section below.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy