Storage Classes in C

Storage Classes in C


Friends we will be going step by step learning C, will find meaning of each new term when we get introduced to it while learning storage classes.

Storage is related with

  • Scope of a variable: It is the boundary within which a variable can be used.
  • Memory allocated by compiler for storing variable


Storage class defines the scope and lifetime of a variable. With C complier view we can say that variable name identifies physical location from a computer where variable is stored. As we know that variable inside a function is automatic by default therefore ‘auto’ keyword is rarely used.

There are always 2 memory locations in a computer system where variables are stored, these are: Memory and Registers of CPU.


Why to use Storage Classes?


  • To determine the location of a variable.
  • To set initial value of a variable or if not specified then default value.
  • To define scope of a variable.
  • To determine the lifespan of a variable.


General Types of Storage Classes:


  • Automatic Storage Class
  • Register Storage Class
  • Static Storage Class
  • External Storage Class


Auto Storage Class:

This is a default storage class specified for variables defined inside a function. All the variables declared inside the block is also auto variable. Auto storage class means a local variable.

Use of keyword auto before the variable declaration is optional because all the variables declared inside a function are by default auto.  Auto keyword can be used only inside functions. Memory for auto variables is on the stack. Auto variables contain garbage values until explicitly initialized. Variables which are declared auto will automatically have their storage allocated on entry to a function and deallocated when the function exits.



int Demo(int x, int y)


int l;

auto int z;


// In this example x, y, l, z are auto variables.

// Function parameters: local variables (auto).


Register Storage Class:

Register variables are stored in the processor registers and they can be accessed and manipulated faster. Other storage class variables are stored in memory. This storage class specified is used to indicate to the compiler that the variable will be used frequently and it should be placed in a CPU register.

Use of register storage class is request, if the CPU registers are available then they are stored inside the register and if not they are treated as auto storage class variables. Register storage class variables must be defined inside the block or function. It is not allowed to declare register storage class variables globally.

Register variables contains garbage values until explicitly initialized. Register storage class variables are accessible inside the block or function only means they are having the local scope. Use the register storage class for integer and character only, in case of other data types like float and double it gives unexpected output because registers unable to store floating point values.



int Demo()

{ register int k=10;}


Storage Classes in C


Extern Storage Class:

All the variables of auto and register storage class had a limited scope means they are accessible in the block in which they are declared. So in some applications it may be useful to have data which is accessible from within any block, and which remains in existence for the entire execution of the program.

To get the above requirement we use global variables. Storage class for the global variable is extern. The scope of external variables is global, i.e. the entire the source code in the file following the declaration. Extern (global) variables are by default initialized with zero.

Memory allocated for the global variable is on B.S.S.(Block Starting with Symbol) or non BSS(Block starting with value) section. Memory allocated for extern variables is when the program begins execution and remains allocated until the program terminates.

If the variable is initialized then it stored in non BSS section and non initialized variables are stored in BSS section. If the extern variable is declared and defines at beginning of file and used inside the single source file then there is no need to write the keyword extern.

If the program is divided in several source files and a variable is defined in let say file1.c and used in file2.c and file3.c then extern keyword must be used in file2.c and file3.c. Both the files file2.c and file3.c shares the same copy of the variable. Above same concept is applicable for the functions also.



int global = 10; //Global variable

int main()


return 0;



Static Storage Class:

Static storage class specifier is used for two main purposes for local variables and global variables. Means we can use the term local static variable and global static variable. Lifetime of the static variable is entire the program. But scope of the static variable is inside the file only in which it is declared.

Static variables are by default initialized with zero. Static variables have internal linkage For local variables: when a local variable is defined with the static storage class specifier, it is initialized to 0 by default in the absence of an explicit initialization value. Static variables are initialized only once in the program.

Static variables in a function definition will retain its value across multiple calls to the function. So for global variables and functions, when a global variables or functions when defined as static, its scope is reduced to the C program file in which it is defined. Even though it is a global variable, it cannot be accessed from another module (it is said to have internal linkage). Because when we declare a variable globally it can be accessible from outside the file also.



static int x = 10; //Global static variable

int main()


static int y = 20; //Local static variable

return 0;


For more reading about technology news in singapore and seo to online marketing do view more about other pages.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc.

Leave a Reply