The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

C Storage Classes

In C programming language, storage classes are used to define things like storage location (whether RAM or REGISTER), scope, life time and default value of a variable.

In C programming language, memory of variables is allocated either in computer memory (RAM) or CPU Registers. The allocation of memory depends on the storage classes.

In C programmig language, there are FOUR storage classes and they are as follows...

  1. auto storage class
  2. extern storage class
  3. static storage class
  4. register storage class

auto storage class

The default storage class of all local varibles (variables declared inside block or function) is auto storage class. Variable of auto storage class has the following properties...

Property Description
Keyword auto
Storage Computer Memory (RAM)
Default Value Garbage Value
Scope Local to the block in which the variable is defined
Life time Till the control remains within the block in which variable is defined

Example Program 1

#include<stdio.h>
#include<conio.h>

int main(){
    int i;
    auto char c;
    float f;
    printf("i = %d\tc = %c\tf = %f",i,c,f);
    return 0;
}

Example Program 2

#include<stdio.h>
#include<conio.h>

int main(){
    int a=10;
    {
         int a=20;
         printf("%d",a);
    }
    printf(" %d",a);
    return 0;
}

Example Program 3

#include<stdio.h>
#include<conio.h>

int main(){
    {
         int a=20;
         printf("%d",a);
    }
    printf(" %d",a);  //a is not visible here
    return 0;
}

External storage class

The default storage class of all global varibles (variables declared outside function) is external storage class. Variable of external storage class has the following properties...

Property Description
Keyword extern
Storage Computer Memory (RAM)
Default Value Zero
Scope Global to the program (i.e., Throughout the program)
Life time As long as the program’s execution does not comes to end

Example Program 1

#include<stdio.h>
#include<conio.h>

int i;    //By default it is extern variable
int main(){
    printf("%d",i);
    return 0;
}

Example Program 2

#include<stdio.h>
#include<conio.h>

extern int i;    //extern variable
int main(){
    printf("%d",i);
    return 0;
}

Static storage class

The static storage class is used to create variables that holds value beyond its scope till the end of the program. Static variable allows to initialize only once and can be modified any number of times. Variable of static storage class has the following properties...

Property Description
Keyword static
Storage Computer Memory (RAM)
Default Value Zero
Scope Local to the block in which the variable is defined
Life time The value of the persists between different function calls (i.e., Initialization is done only once)

Example Program 1

#include<stdio.h>
#include<conio.h>

static int a;
int main(){
    printf("%d",a);
    return 0;
}

Example Program 2

#include<stdio.h>
#include<conio.h>

static int i=10;
int main(){
    i=25;       //Assignment statement
    printf("%d",i);
    return 0;
}

Register storage class

The register storage class is used to specify the memory of variable has to be allocated in CPU Registers. The memory of register variable is allocated in CPU Register but not in Computer memory (RAM). The register variables enables the faster accessability compared to other storage class variables. As the number of registers inside CPU are very less we can use very less number of register variables. Variable of rregister storage class has the following properties...

Property Description
Keyword register
Storage CPU Register
Default Value Garbage Value
Scope Local to the block in which the variable is defined
Life time Till the control remains within the block in which variable is defined

Example Program 1

#include<stdio.h>
#include<conio.h>

int main(){
    register int a,b;
    scanf("%d%d",&a,&b);
    printf("%d  %d",a,b);
}

The following table provide detailed properties of all storage classes...

Storage Class Keyword Memory Location Default Value Scope Life Time
Automatic auto Computer Memory (RAM) Garbage Value Local to the block in which the variable is defined Till the control remains within the block in which variable is defined
External extern Computer Memory (RAM) Zero Global to the program (i.e., Throughout the program) As long as the program’s execution does not comes to end
Static static Computer Memory (RAM) Zero Local to the block in which the variable is defined The value of the persists between different function calls (i.e., Initialization is done only once)
Register register CPU Register Garbage Value Local to the block in which the variable is defined Till the control remains within the block in which variable is defined

Place your ad here
Place your ad here