Introduction To Storage Classes In C Language

Prashant | Sun, 14 Jun, 2020 | 190

Introduction to storage classes in C language

In this article we will learn about storage classes in C language and their types.

Storage Classes

The storage classes are generally used to specify the different features of variable/function. The features govern various aspects of the variables or function which include the scope, visibility, and lifetime, which in turn assists us in knowing the functionality of the particular variable during the runtime of the program.

A variable's storage class tells us the following,

  1. Where the variables would be stored?
  2. What will be the initial of the variable, if the initial value is not specifically assigned? (i.e. the default initial value).
  3. What is the scope of the variables, i.e. in which part of the program of the functions the value of the variable would be available?
  4. What is the life of the variable, i.e. how long the variable exists?

Types of storage classes in C

There are four classes in C programming language,

  1. Automatic storage classes
  2. Register storage classes
  3. Static storage classes
  4. External storage classes

1) auto

Any variable which is declared inside a function or block is by default assigned an auto class also called automatic variable. So it is not much necessary to separately call a variable as auto. The auto variables are only accessible within the block or function in which they are declared. In case you want o access these variables outside their scope or block, this can be done by using the concept of pointers, by pointing to the address of the variable which is to be accessed.

#include <stdio.h>

int main ()
{
	auto int i,j;
	
	printf("\n%d %d",i,j);
	
	return 0;
}

2) extern

Any variable which is declared outside a function is defined as an extern variable. The variable should not be defined in the same block, or else it won't be regarded as an extern variable. It is also referred to as global variable. If a variable with the same name as that of an extern variable is declared inside a block or function, then the local variable will get more preference as compared to the extern variable. As a result, extern variable will be hidden in that case.

#include <stdio.h>

/*Global variable*/
int a=10;   

void abc()
{
	printf("%d\n",a);
	a=a+10;
}

int main()
{
	a=a+5;
	printf("%d\n",a);
 
	a=a+20;
	abc();
	printf("%d\n",a);
	
	abc();
	a=a+20;
	
	abc();
	printf("%d\n",a);
	
	return 0;
}

3) static

The main property of static variables is that they can retain their values even when they are used out of their scope. So, they are able to store the value of their last use in their scope. The static variables are initialized only once and they exist throughout the program execution. Therefore, memory is also allocated only once as there is no need to redeclare the variables. The scope of these Local Static variables are local to the block or function in which they are declared. On the other hand, the scope of Global Static variables is throughout the program execution. If a static variable is not explicitly initialized, it is assigned value 0 by the compiler.

#include <stdio.h>

void  abc()
{
	static int a=10;
	printf("%d\n",a);
	a=a+10;
}

int main()
{
	abc();
	abc();
	abc();
}

4) registers

The register storage classes have the same functionality and scope as that of auto variables, but the only difference is caused by the way the registers are stored. The compiler stores these variables in the high-speed register memory of the microprocessor. The variables which are very frequently used by the compiler are stored in register memory, as the register have very limited memory. If the register memory exceeds then the compiler automatically converts the register variable to auto variable. We can not obtain the address of register variable using pointers. The scope and lifetime of register variables are identical to the auto variables.

Syntax

 register int a;
#include <stdio.h>

int main()
{
	register int i;

	for(i=1;i<=100;i++);
		printf("%d",i);

	return 0;
}

Syntax:

 storage_class var_data_type var_name;

0 comments
Leave a comment