Storage Classses in C

Storage classes of C provide the following information to the compiler:

  • Storage area of a variable 
  • Scope of the variable (Visibility of a variable)
  • life of a variable (How long that variable will be there in active mode)
  • Default value of a variable 

There are four main storage class types:

  1. Auto
  2. Register
  3. Extern
  4. Static

 

Auto Storage Class-

  • Auto storage class variables are created automatically and destroyed automatically.
  • This storage class variable is stored in the stack area of the program memory.
  • Scope of the Auto variable is restricted within the function body.

Working with Auto variables-

#include<stdio.h>
void demo(void);

int main(){
// calling demo function demo(); demo(); demo(); return 0; }

void demo(){
int a=5;
++a;
printf("a=%d\n",a);
}

Output-

a=6
a=6
a=6

Here we are calling the demo function from the main. an auto variable is restricted within the function body that's why it is destroyed when we moved outside of the demo function.
The lifetime of auto variable is within the function body only

 

Register Storage Class-

  • The register storage class is used to store the variables in the CPU register rather than RAM.
  • Storing the variables in the CPU registers will result in quick availability of them.
  • Register variables can only be used within the limited scope and ‘register’ keyword is used for declaring.
  • Register memory is limited so you cannot create many register variable.

Let’s see the below code example for better understanding.

#include<stdio.h>
int main(){
	register int tmp=10; //Using the register keyword for declaration
printf("value of tmp=%d\n",tmp)
return 0;
}

 

Extern Storage Class-

The extern storage class is utilized to share a reference of a global variable that is visible to all the files of a program.
The ‘extern’ keyword declares a global variable or a function in another program file for providing the reference of either variable or function defined in the main program file.

Let’s see the below code example for better understanding.

Main file: main.c
#include<stdio.h>
int check;
extern void extern_func();
main(){

	check=5;
	extern_func();
return 0; }
Another file: other.c

extern int check;

void extern_func(void){
	printf(“The value of check is %d\n”, check);
}

 

Static Storage Class-

The static storage class tells the compiler to keep a local variable that persists during the life-time of the program So making local variables static permits them to store their values in between the function calls.
We can also apply the static modifiers to global variables. This stops the scope of the variable to be used outside the declared file.
Let’s see the below code for better understanding:

#include<stdio.h>
void my_funct(void); // Declaration of function
static int check=1; //global variable

int main(){
	while(check<5){
		my_func();
		check++;
	}
printf(“The value of i is %d and check is %d\n”, i, check); 
return 0;
}

void my_func(void){
static int i=3; //local static variable
i++;
}

Output:
The value of i is 4 and check is 1
The value of i is 5 and check is 2
The value of i is 6 and check is 3
The value of i is 7 and check is 4

So as a conclusion we studied the storage class present in C, and the types of storage class through this blog. I hope you were able to understand the concept and the code clearly.

 

Interview Questions on storage classes in C

 

1-What is the default C storage class for a variable?
Automatic storage class is the default storage class in C for all local variables.

2-Where will the space be allocated for an automatic storage class variable?
Auto storage class store variable in memory. if any variable is defined as an auto storage class then memory is allocated in the stack segment of the program.

3-Where are static variables stored in C?
Static variable store at the data segment of the program memory.

4-Which storage class is used for faster execution?
Register class because CPU register takes less time than memory to access the variable.

5-Where are extern variables stored?
Extern variables are stored at the data segment of the program memory.