Storage class in C Language | auto , register, extern, static in C Language

You must first complete What is a variable in C Language | Variable naming rules in C Language before viewing this Lesson

Storage classes in C Language | auto , register, extern, static in C Language

Storage class specifies the scope of a variable in a program. It specifies the whether the variable will be stored in RAM or registers. It specifies which parts of a program can access the variable and how long the variable stays in the memory. Storage class in C Language is of four types.

  • auto 
  • register 
  • static 
  • extern 

1.  Storage Class auto in C Language

Keyword auto is used to specify automatic variables in C Language. Variables declared without any storage class are also automatic variables. They are local to the function in which they are declared.

Example:

int var;

or

auto int var;

Program to demonstrate the use of automatic variables in C Language

#include<stdio.h>
int main()
{
  auto int var=20;  /*or int var;*/
  printf("\nvar=%d",var);
  return(0);
}
Output
i=10

2. Storage Class register in C Language

Keyword register is used to specify register variables in C Language. They are stored in registers inside CPU in spite of RAM where other variables are declared. Their processing is fast as compared to other variables. 

Only those variables should be of register type which are used  again and again in program just like loop control variables.

Example

register int var;

In the above declaration, a is an int type variable which has been defined in the computer register.

Program to demonstrate the use of register variables in C Language

#include<stdio.h>
int main()
{
  register int var=100;
  printf("\ni=%d",var);
  return(0);
}
Output
i=10

3. Storage Class static in C Language

Keyword static can be used to declare static variables in C Language. they are special variables which keep their values alive throughout the program. They get initialized only once.

Program to demonstrate the use of static storage class in C Language

#include<stdio.h>
void showstatic()
{
  static int var=10;
  var++;
  printf("\ni=%d",var);
}
int main()
{
  showstatic();
  showstatic();
  showstatic();
  return(0);
}
Output
var=10
var=11
var=12
Description
In the above program, when  the function showstatic () is called for the first time, static variable var is initialized with value 10.

When this function is called again, variable var will not be initialized again with 10 but it will take its previous value and increment it further with one with each call to function showstatic ().

 4.  Storage Class extern in C Language

Keyword extern can be used to declare an external variable. They are special types of variables which keeps their values alive throughout the program. They are always declared outside a function.

We can make any variable global by using the keyword extern anywhere in a program. Eternal variables can be accessed across multiple files. 

Program to demonstrate the use of extern storage class

#include<stdio.h>
void show();
void  demo();
int main()
{
  extern int var;
  printf("\nvar=%d",var);
  show();
  demo();
  return(0);
}
void show()
{
  extern int var;
  printf("\nvar=%d",var);
}
int var=10;
void demo()
{
  var++;
  printf("\nvar=%d",var);
}
Output
var=10
var=10
var=11
Description
In the above program, variable var has been defined after show() function, it can’t be directly  within the main() function.
But it has been made available within main() function by redeclaring the variable var within the main() function by using the keyword extern. The variable var will retain its value throughout the program.

 

Spread the love
Lesson tags: auto in c, automatic variable in c, extern in c, register in c, register variable in c, static in c, static variable in c, Storage Class auto in C Language, Storage Class extern in C Language, Storage Class register in C Language, Storage Class static in C Language
Back to: C Programming Language
Spread the love