Skip to main content

27. Storage Classes

 

Storage Classes in C

Storage classes in C are used to determine the lifetime, visibility, memory location and initial value of a variable. There are four types of storage classes in C:

  • Automatic
  • External
  • Static
  • Register
Storage ClassesStorage PlaceDefault ValueScopeLifetime
autoRAMGarbage valueLocalTill the end of the function block where they are defined in.
externRAMZeroGlobalTill the end of the main program. Maybe declared anywhere in the program.
staticRAMZero or
NULL
LocalTill the end of the main program. Retains value between multiple functions call.
registerRegisterGarbage valueLocalTill the end of the function block where they are defined in.

Automatic Variable

Automatic variables are allocated memory automatically at runtime. The memory assigned to automatic variables gets freed upon exiting from the block. Every local variable is automatic in C by default. 

#include <stdio.h>
int main()
{
   int a; // automatic variable , (int a) and (auto int a) both are same
   char b;
   float c;
   printf("%d - %c - %f", a, b, c); // Printing initial default value of automatic variables a, b, c
   return 0;
}

Output :

0 - - 0.000000 // Garbage values (any random number)

External Variable

They are same as global variables. These variables are declared outside any function. A global variable can be changed by any function in the program. It is recommended to minimise the use of unnecessary global variables in a program.

extern keyword is used to inform our C compiler that a given variable is declared somewhere else. Using extern will not allocate space for the variable. It is only declaration and intended to specify that the variable is declared elsewhere in the program. We can only initialize the extern variable globally, i.e., we can not initialize the external variable within any block or method.

#include <stdio.h>
int a;
int main(){
   extern int a;
   printf("%d", a);
   return 0;
}

Output :

Static Variable

The variable defined as static specifier can hold their value between the multiple function calls. A same static variable can be declared many times but can be assigned at only one time. It is recommended to minimize the use of unnecessary static variables in a program.The keyword used to define static variable is static. 

#include <stdio.h>
static char c;
static int i;
static float f;
static char s[100];
int main()
{
printf("%d %d %f %s", c, i, f, s[100]); // initial default value of c, i, f, s will be printed.
return 0;
}

Output :

0 0 0.000000 (null)

Register

The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU. Generally, this is done for the variables which are being used frequently. We can't dereference the register variables i.e. we can't use & operator for the register variables. We can store pointers into the register i.e. a register can store the address of a variable.

#include <stdio.h>
int main()
{
   register int e;
   printf("%d", e);
   return 0;
}

Output :

// Garbage Value (any random number)

Comments