C Storage class

In this tutorial, you will learn about the storage class which is very exciting as it grants us to change the feature of variables in C. Storage Classes describe the features of variables and functions. It will help to trace the existence of the variable during the runtime of the program. The storage class in C will help to decide the following things:

Scope:

The storage class in C will help to determine the scope which is also known as the visibility level of the variable. It specifies the availability of the value of the variable would be available inside a program.

Default initial value: 

The Storage class in C will decide the part of storage for allocating memory for the variable. It determines the variable’s storage location i.e. memory and registers. Where memory and CPU registers are the type of memory locations where we store the value of variables. If we do not explicitly initialize the variable then the storage class will define its default initial value.

The lifetime of the variable:

Storage class in C determine the lifetime of the variable if the variable is ‘local’ or ‘global’.  It will help to decide how long will the variable exist.

What are the types of storage classes?

The storage classes or specifiers in C tells the compiler how to store subsequent variables. The general syntax of a variable declaration that uses storage class is shown below:

Storage_specifier data_type variable;
We use the following four different storage classes most often in our program.
  • Automatic storage class
  • Register storage class
  • Static storage class
  • External storage class

Below you see the detail of all the storage classes in C.

What are Automatic storage class?

Auto storage class are the default storage class for all variables. At the time of defining and declaring variables inside a function or a block, they are all by default belong to the automatic storage class. This makes the keyword auto rare while writing programs in C language. The scope of the Variables having automatic storage classes is local to the block wherever they are defined in. Eventually, the variables are destroyed automatically upon the exit from the block so we cannot access them outside the block.  However, We can access them outside their scopes by using the concept of pointers. The pointer will help on by pointing the exact memory locations where the variable resides. Whenever we declare auto variables we assign them with a garbage value by default. Following in the syntax of the auto static variable.
Syntax:
auto var_datatpe var_name

Now, look at the following example which will help you to understand more of the use of auto variables.

Code:

Output:

3
4

In the above program, we see two variables. Both these variables are defined within the same storage class ‘auto’.  We can use them only within the function i.e. local variables. Here, you have to note that you have to initialize variables properly otherwise you are likely to get unexpected results as auto variables are assigned garbage values by the compiler.

What are Register storage class?

Register variables will inform the compiler to store the variable in CPU instead of memory. It will give hints to the compiler that the declared variable will be accessed frequently. We keep a few variables in a register which needs faster accessibility than the normal variables. For example, we can use registers in a loop where we will have to use the variables number of times int the program in a very short span of time. Hence the use of register will help to the running time of the program.

We can use register specifier which declares the variable of register storage class. Similar to the Auto variables, register variables are local to the block they are defined in and gets destroyed on exit from the block. The compiler will not give any initial values to the register variables. Also while declaring the register variable, we do not need to give the unary &(address of) operator explicitly or implicitly. The syntax for the register storage class is given below.

Syntax:

register var_datatype var_name;
Now go through the example given below. This example will help to understand more about the register storage class in C.
Code:

The above code will display an error message as the address of variable x is given to the variable p. Here x is declared as a register variable so the code won’t succeed as the address of register variables could not be passed. One thing you need to keep in mind is that we can never get an address of register variables. Another key point here that every register variables are not necessarily stored in the registers. The CPU will have a limited number of registers which will have more important things to do. Therefore, sometimes they won’t be free. In such a context, the register variables work as if its storage class is auto.

What are static storage class?

Static variables are popularly storage class which will tell the compiler to persist/save the variable until the end of the program. The static variables will preserve the values even when they are out of scope. The static variable will exist until the end of the program. So you will not have to declare the same variable again and again when it goes out of scope. Upon defining static variable the program will allocate no new memory as we will not redeclare the variables. Depending upon the place of the declaration, our static variable can be either external or internal. The scope of internal static variables will remain local to the function in which they were defined. We can access the external static variables anywhere in the program. By default, the static variables are assigned with the value 0 by the compiler. Following is the syntax of the static variables.

Syntax:

static var_datatype var_name;
Now let’s see our first example using the static storage class in C. The example will help you to understand much about the implementation of static variables in C.
Code:

Output:

0 1 2 3 4
In the above code, we have a variable ‘counter’ which is declared as a static variable. Here the value of the counter increases every time when we call the function. The variable count will not be initialized for every time the function is called.

What are External storage class?

We can use the extern specifier which will give the declared variables an external storage class.  The principle use of external storage class is to inform the compiler that the variable is declared somewhere else. For understanding the importance of extern variable it is necessary to understand the difference between the definition and declaration. Declaration process will declare the name and type of the variable or function. Whereas definition allocates storage for the variable or the body of the function to be defined. The extern declaration does not allocate storage for variables. The extern variables give us permission to access the variables between two different files which are the part of a large program.  Following is the syntax of the extern variable in the program.

Syntax:

extern var_datatype var_name;

Now go through the following example which demonstrates the use of extern in our programs. In below example, we will be building two files with the names first.c and secind.c respectively. We will declare a global variable in the file ‘first.c’  which will be used in another file ‘second.c’ by using the extern keyword. Look at the code below to understand the implementation of the extern keyword in more detail.

Code:

first1.c

second.c

We will run the file ‘second.c’  where this file will use the global variable of ‘first.c’ using the concept of the external storage class. The above code will generate the following output.

Output:

The value after calling the function is: 11

Choosing the storage class:

At the present time, writing the code is not an ultimate solution for achieving the result. A good programmer must have to consider different things for better performance. We have to improve the speed of execution of programs and carefully use the memory space occupied by the variables.  It’s important to remember the following things while using the storage classes:

  1. We use auto variables in the condition when we do not have the intention to use any of the other storage classes.
  2. static variables come into the play in the condition when we want the value of the variable has to be constant for every time we call it using different function calls.
  3. We can use the register variables when will have to use the variables frequently. As CPU will have limited registers so we have to be careful while using this storage class.
  4. extern variables are the best option for the condition when we will have to use the variable in all functions of the program.
SHARE C Storage class

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Share