Storage Classes in C

by | Aug 28, 2022 | C

Introduction

So, now we all are at the end of the series of our C language. At the beginning of this course, we talked about variables. Definition, declaration, and initialization of variables are the topics that we have covered already.

Variables that are used in programming languages have some unique features.

Each variable has a storage class that defines the features of that variable.

Now you may wonder what type of features we are talking about.

The storage class is the one that tells the compiler about where to store the variable, its initial value, what is the scope of that particular variable, and whether it is a global or a local variable.

There are mainly four storage classes in C

1. auto

2. extern

3. static

4. register

Before proceeding, let’s discuss global and local variables.

Local variables: The variables that are declared inside a function or a block (a group of statements written inside curly braces constitutes a block of code) are termed Local variables. Hence, they are local to a particular function and can be used inside that function only.

Global variables: The variables that are declared outside all the functions and can be accessed by any function or we can say that the variables that are accessed globally in a program are termed as Global variables.

Now let’s understand each of the storage classes one by one.

auto

Local variables belong to the ‘auto’ storage class. All the variables declared inside a particular function are local variables because these can only be accessed inside that function. Such variables belong to the ‘auto’ storage class and while making a declaration there is no need to write the ‘auto’ keyword before them because these are ‘auto’ by default.

We all are familiar with such variables since we had used them in our entire C language course. But we never use ‘auto’ while making the declaration due to the reason discussed above.

Program:

#include<stdio.h>
int sum(int auto n, int auto m)
{
int auto s=n+m;
return s;
}
int main()
{
int auto x,y;
printf("Enter two values to find sum:\n");
scanf("%d %d",&x,&y);
int auto S=sum(x,y);
printf("Sum is:%d",S);
return 0;
}

 

Output:

In the above code, we have two functions. One of them is the ‘sum’ function used to calculate the sum of two integers and the other one is the ‘main’ function which is necessary and sufficient to run a program.

Now the variables ‘n’ and ‘m’ passed as arguments in the ‘sum’ function definition as well as the variable ‘s’ declared inside the ‘sum’ functions, are local to that particular function. Thus, they belong to the ‘auto’ storage class.

Similarly, the variables declared inside the ‘main’ function are local to the ‘main’ function only and belong to a storage class named ‘auto’.

Since we already discussed that it is not necessary to write ‘auto’ with such variables, it is written in the above code just to make you understand the ‘auto’ storage class.

The code will definitely work without the use ‘auto’ keyword.

Program:

#include<stdio.h>
int sum(int n, int m)
{
int s=n+m;
return s;
}
int main()
{
int x,y;
printf("Enter two values to find sum:\n");
scanf("%d %d",&x,&y);
int S=sum(x,y);
printf("Sum is:%d",S);
return 0;
}

 

Output:

The same code is written without using the ‘auto’ keyword.

extern

‘extern’ storage classes deal with global variables. Using the ‘extern’ keyword before any variable tells the compiler that this particular variable is declared somewhere in the program and can be accessed anywhere in the program. Thus, the variables with the ‘extern’ keyword can be treated as a global variables.

The most important feature of a global variable is that it is declared outside of all the functions and can be accessed throughout the program. Moreover, its value can be changed anytime within any function.

Program:

#include<stdio.h>
int x=87;
int show()
{
printf("Enter the value of x:\n");
scanf("%d",&x);
printf("Value of x in show function is: %d",x);
}
int main()
{
printf("Enter the value of x:\n");
scanf("%d",&x);
printf("Value of x in main function is: %d\n",x);
show();
}

 

Output:

Go through the above code, in which we declare a variable ‘x’.

Since ‘x’ is defined outside of all functions. Thus, it is a global variable with an initial value of 87.

In the ‘main’ function the value assigned to ‘x’ is 100, on the other hand, the value assigned to the same variable ‘x’ in the ‘show’ function is 200.

This proves that ‘x’ is a global variable, and we can assign any value to it according to our choice.

Until now, we didn’t use the ‘extern’ keyword.

Let’s understand the use of the ‘extern’ storage class with global variables.

Like all other variables, global variables are also allocated with some space in the memory. We can assign values to them in the same program in which they are declared as we did in the above code.

But C language also provides us a way to use such variables in other programs also. We can declare a global variable in one program file and assign it a value in another program file. This is possible with the use of the ‘extern’ keyword.

Suppose we have a program file named firstfile.c. Let’s declare a global variable in this program file as shown below.

int p=0;

Now, the same variable ‘p’ will be declared with an ‘extern keyword in a header file named headerfile.h

extern int p;

Finally, we will include one more file in which we will use this globally declared variable ‘p’.

Suppose the name of the file is the second file and is saved as secondfile.c

To use this global variable ‘p’ we need to include the header file in it by writing the statement written below

#include<firstfile.h>

Now, we will assign some value to the variable ‘p’ by writing the code in the file name

secondfile.c

Program:

#include<firstfile.h>
main()
{
p=50;
printf(“%d”,p);
}

 

Static

A variable declared with the ‘static’ keyword has multiple features and can be used in multiple ways according to the demand of the time.

A variable declared with the keyword ‘auto’ exists until the function (in which it is declared) exists. As soon as the function ends the variable gets destroyed. But this is not the case in the ‘static’ storage class.

Once a variable declared with the ‘static’ keyword is initialized, exists till the end of the program.

Also, if a static variable is declared inside a function that doesn’t mean that it will get destroyed when the program ends. It remains in existence till the end of the program regardless of being concerned about the function.

A static variable is declared outside of all the functions in a program, then it can be used only in the program in which it is declared. It is not possible to access the same variable in another program file by using the ‘extern’ keyword.

Program:

#include<stdio.h>
static int x=87;
int show()
{
static int i=0;
printf("x=%d\n",x--);
printf("i=%d\n\n",i++);
}
int main()
{
while(x>=82)
{
show();
}
return 0;
}

 

Output:

In the above code, ‘x’ is a global variable declared with the ‘static’ keyword and initialized with value 87.

In the ‘main’ function there is a local variable declared with the ‘static’ keyword and initialized with value 0.

The benefit of writing ‘static’ before the variable ‘i’ is that its value is not getting destroyed when the function ‘show’ ends. If we had not written ‘static’ before the declaration of ‘i’, then every time the function ‘show’ would have been called, ‘i’ would have been declared every time with an initial value of 0 and as the function ‘show’ would exit, it would also have got destroyed.

See the code written below without the use of the ‘static’ keyword and observe the changes in the output.

Program:

#include<stdio.h>
int x=87;
int show()
{
int i=0;
printf("x=%d\n",x--);
printf("i=%d\n\n",i++);
}
int main()
{
while(x>=82)
{
show();
}
return 0;
}

 

Output:

register

Variables declared with the ‘register’ keyword get stored in a register instead of memory (RAM). Register storage class declares variables with the ‘register’ keyword, and they have the same functionality as that of the ‘auto’ variables.

The key difference between them is that the compiler tries to store such variables in a microprocessor register if there is a free register available.

The access of variables declared with the ‘register’ keyword is much faster than the normal variables.

Moreover, not all the variables defined with the ‘register’ keyword get stored in a register. Since it depends upon the availability of free registers. If a free register is not available then they are stored in the memory only.

Only a few variables are declared with the ‘register’ keyword, which is to be accessed frequently and thus improves the program’s running time.

Therefore, the storage of variables in the register depends upon various restrictions of implementation and hardware.

It is important to note that we cannot access the address of variables declared with the ‘register’ keyword because they are not stored in memory, and as a result, they do not have a memory location.

Program:

#include <stdio.h>
int main()
{
register int n = 20;
int *ptr;
ptr = &n;
printf("address of n : %u", ptr);
return 0;
}

 

Output:

 

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.