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.
Contents
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:
- 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?
Now, look at the following example which will help you to understand more of the use of auto variables.
Code:
#include<stdio.h> int main() { int num=3; auto int number=4; printf("%d\n",num); printf("%d",number); }
Output:
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:
#include <stdio.h> int main() { register int x = 100; int *p = &x; //error displayed: address of register variable requested printf("The value of i: %d", *p); printf("The address of i: %u", p); }
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:
#include<stdio.h> void demonstration() { // static variable static int counter = 0; printf("%d ",countera); // value is updated and // will be carried to next // function calls counter++; } int main(){ for (int i=0; i<5; i++) demonstration(); return 0; }
Output:
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:
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
#include<stdio.h> extern int a=10; void function(){ a++; printf("The value after calling the function is: %d", a); }
second.c
#include"first.c"; int main(){ extern int a; function(); }
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:
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:
- We use auto variables in the condition when we do not have the intention to use any of the other storage classes.
- 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.
- 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.
- extern variables are the best option for the condition when we will have to use the variable in all functions of the program.