Home » C Tutorial » Storage Classes in C with Examples

Storage Classes in C with Examples

Storage classes in C tell where the variable should be stored in the memory? What will the variable store at the time of declaration? What will be the variable’s scope? If the variable is local, it will access only locally, or if the variable is declared globally, it should be accessed from anywhere in the program. Also, it will tell the lifetime of the variable.

There are two types of locations in a computer where such a value is stored

  1. Memory
  2. CPU registers

The storage classes in C will decide where the variable should store.

Types of storage classes in C

There are four types of storage classes in C

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

These are the different storage classes in C. We will see these storage classes one by one with examples.

1. Auto (Automatic)

Auto is the default storage class in C for local variables. 

Syntax

auto data_type variable_name;

Features

Storage locationMemory
ScopeLocal to the block in which it is defined.
LifetimeTill the control remains within the block where it is defined.
Default initial value Garbage value.

Example 1

int demo(int a)
{
	int cnt;
	auto int x;
}

The example above defines two variables with the same storage class, and auto can only be used within functions, i.e. local variables.

The auto variables are created when the function is called and destroyed automatically when the function is exited, hence the name automatic.

Example 2

#include <stdio.h>  
int main()  
{  
	auto int x;  
	auto char y;  
	auto float z;   
	printf("x= %d\ny=%c\nz=%f",x,y,z); 
	return 0;  
}

Output

x= 0
y=
z=0.000000

2. Register

The register storage class in c is used to define local variables that should be stored in a CPU register instead of memory. This means that the variable has a maximum size equal to the register size (usually one word) and can not have the unary ‘&’ operator applied to it (as it does not have a memory location).

Defining a variable with the register class does not mean that the variable will be stored in a register. It is entirely up to the compiler to decide where a variable is to be stored in the register or memory.

Syntax

Register data_type variable_name;

Features

Storage locationCPU Registers.
ScopeLocal to the block in which it is defined.
LifetimeTill the control remains within the block where it is defined.
Default initial value Garbage value.

Example

#include <stdio.h>  
int main()  
{  
register int x;
printf("%d",x); //this will print the garbage value
}

Output

276689280

The register storage class in c should only be used for variables that require quick access, such as counters.

3. Static 

The static is the default storage class in c for global variables. Static can also be defined to local variables (within a function), and they retain their values between calls to the function. We can also declare a static function in C. Static storage class will store the value statically instead of automatic memory allocation.

Syntax

Static data_type variable_name;

Features

Storage locationMemory.
ScopeLocal to the block in which it is defined.
LifetimePersists between different function calls.
Default initial value Zero.

Example

#include <stdio.h> 
int demo()  
{  
    static int cnt=0;  
    cnt++;  
    return cnt;  
}   
int main()  
{  
   printf("%d",demo());  
   printf("\n%d",demo());  
   printf("\n%d",demo());
   return 0;  
}

Output

1
2
3

In the above example, we can see that the count variable gets incremented by one. If we declare the variable normal, the value stored in it will get deleted. 

Difference between auto and Static storage class.

AutoStatic
Auto variables persist till the control remains within the block where it is defined.Static variables persist in different function calls.
The auto variables are created when the function is called and destroyed automatically when the function is exited.The variables persist in the main memory.
Auto is the default storage class for local variables.Static is the default storage class for global variables.
Default initial value: Garbage value.Default initial value: Zero
An auto declaration is valid only for variables.A static declaration is valid for variables and functions.
Declaration: auto int x;Declaration: static int x;

4. Extern

An extern defines a global (external) variable that is accessible by any function in the program. They are declared outside all functions. Note that the extern declaration does not allocate storage space for variables.

Syntax

extern data_type variavle_name;

Example

#include <stdio.h>
extern int x = 18;
int y = 20;
int main() 
{
   extern int b;
   printf("The value of extern variables x and y: %d,%d\n",x,y);
   x = 15;
   printf("The value of updated extern variable x: %d\n",x);
   return 0;
}

Output

The value of extern variables x and y: 18,20
The value of updated extern variable x: 15

Example of using all the storage classes in one program

#include<stdio.h>
void display_term(int x)
{ 
	extern long term; /* x and p are the parameters */
	printf("(%d!=%ld)+",x,term); /*In this function term is external variable*/
}
void cal_term(int p)
{
	int i; /*This is (i) local variable*/
	extern long term; /*In this function term is external variable*/
	term=1;
	for(i=1;i<=p;i++)
		term*=i;
}
void series(int n)
{
	register int i;
	extern long term;
	static long sum=0;
	printf("\n");
	for(i=1;i<=n;i++)
	{
		cal_term(i); 
		display_term(i);
		sum+=term;
}
printf("\b=%ld",sum);
}
long term;
void main()
{
	int x,n;
	printf("Enter the value of n: ");
	scanf("%d",&n);
	series(n);
}

Output

Enter the value of n: 10

(1!=1)+(2!=2)+(3!=6)+(4!=24)+(5!=120)+(6!=720)+(7!=5040)+(8!=40320)+(9!=362880)+(10!=3628800)=4037913

Pin It on Pinterest