C Storage Classes

A storage class represents the visibility and a location of a variable. It tells from what part of code we can access a variable. A storage class in C is used to describe the following things:

  • The variable scope.
  • The location where the variable will be stored.
  • The initialized value of a variable.
  • A lifetime of a variable.

We have four different storage classes in a C program −

  • auto
  • extern
  • static
  • register
 
Auto Storage Class

The variables defined using auto storage class are called as local variables. Auto stands for automatic storage class.

The scope of an auto variable is limited with the particular block only. Once the control goes out of the block, the access is destroyed. This means only the block in which the auto variable is declared can access it.

A keyword auto is used to define an auto storage class. By default, an auto variable contains a garbage value.

Example
 auto int age;
 

 Extern Storage Class

 Extern stands for external storage class. Extern storage class is used when we have global functions or variables which are shared between two or more files.

Keyword extern is used to declaring a global variable or function in another file to provide the reference of variable or function which have been already defined in the original file.

The variables defined using an extern keyword are called as global variables. These variables are accessible throughout the program. Notice that the extern variable cannot be initialized it has already been defined in the original file.

Example
First create a file named variable.h  where you put all your variables with extern keyword 
which can be used by any program by simply including the file name in it

variable.h

extern int num1 = 10;
extern int num2 = 20;
extern.c
#include <stdio.h>
#include "variable.h"
int main()
{
int add = num1 + num2;
printf("%d + %d = %d ", num1, num2, add);
return 0;
} 

Output 




 
Static Storage Class

A static variable is a variable that tells the compiler to retain the value until the program terminates.They are created once when the function is called, even though the function gets repeated it retains the same value and exists until the program terminates.
The default value of static variable is zero(0). The keyword for a variable to declared under static storage class is static.

Example 
 #include <stdio.h>
#include<conio.h>
void display();

int main()
{
display();
display();
getch();
}
void display()
{
static int c = 1;
c += 5;
printf("\t %d ",c);
}
Output 
 


During the first function call, the value of c is initialized to 1. Its value is increased by 5. Now, the value of c is 6, which is printed on the screen.

During the second function call, c is not initialized to 1 again. It's because c is a static variable. The value c is increased by 5. Now, its value will be 11, which is printed on the screen.

Register Storage Class

Register variables are similar to auto variables. The only difference is register variables are stored in CPU register instead of memory.Register variables are faster than normal variables. Mostly programmers uses register to store frequently used variables.Programmers can store only few variables in the CPU register because size of register is less.The keyword for a variable to declared under register storage class is register.

Example 

 #include <stdio.h>
#include<conio.h>
int main()
{
int m1 = 5;
register int m2 = 10;
printf("The value of m1 : %d ",m1);
printf("\nThe value of m2 : %d ",m2);
return 0;
getch();
}

Output 

 



 

 




No comments:

Post a Comment