Tuesday, November 5, 2019

C-Storage-Classes

C Storage Classes


In C programming language, storage classes are used to define things like storage location (whether RAM or REGISTER), scopelifetime and the default value of a variable.
In the C programming language, the memory of variables is allocated either in computer memory (RAM) or CPU Registers. The allocation of memory depends on storage classes.
In C programming 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 variables (variables declared inside block or function) is auto storage class. Variable of auto storage class has the following properties...
PropertyDescription
Keywordauto
StorageComputer Memory (RAM)
Default ValueGarbage Value
ScopeLocal to the block in which the variable is defined
Life timeTill 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...
PropertyDescription
Keywordextern
StorageComputer Memory (RAM)
Default ValueZero
ScopeGlobal to the program (i.e., Throughout the program)
Life timeAs 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 hold value beyond its scope until the end of the program. The static variable allows to initialize only once and can be modified any number of times. Variable of static storage class has the following properties...
PropertyDescription
Keywordstatic
StorageComputer Memory (RAM)
Default ValueZero
ScopeLocal to the block in which the variable is defined
Life timeThe 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 the variable that has to be allocated in CPU Registers. The memory of the register variable is allocated in CPU Register but not in Computer memory (RAM). The register variables enable faster accessibility compared to other storage class variables. As the number of registers inside the CPU is very less we can use very less number of register variables. Variable of register storage class has the following properties...
PropertyDescription
Keywordregister
StorageCPU Register
Default ValueGarbage Value
ScopeLocal to the block in which the variable is defined
Life timeTill 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 provides detailed properties of all storage classes...
Storage ClassKeywordMemory LocationDefault ValueScopeLife Time
AutomaticautoComputer Memory (RAM)Garbage ValueLocal to the block in which the variable has definedTill the control remains within the block in which variable is defined
ExternalexternComputer Memory (RAM)ZeroGlobal to the program (i.e., Throughout the program)As long as the program’s execution does not come to end
StaticstaticComputer Memory (RAM)ZeroLocal to the block in which the variable has definedThe value of the persists between different function calls (i.e., Initialization is done only once)
RegisterregisterCPU RegisterGarbage ValueLocal to the block in which the variable has definedTill the control remains within the block in which variable is defined







No comments:

Post a Comment