Storage_Classes_in_C

Storage classes in C

Storage_Classes_in_C
Storage_Classes_in_C
Advertisements

There are four storage class in c language .

  • auto
  • register
  • static
  • extern

Let’s Look into each and context of their uses.

auto :- auto storage class is the default for variables declared inside a block. Which you initialize regularly while writing your code. Their accessibility is within the block where they are declared. They can also be accessed outside the block if you know the exact memory address where it gets allotted in the stack. Their life is within the block where it is declared.

register :-register storage class is almost the same as auto, the difference is register[r0-r12 ARM processors] get allocated only if they are available else it will be stored in memory. Generally, Variable which is heavily used for processing uses register storage class, It improves processing speed. Things to note is that we can’t get the address of the register using pointer in C language but it is allowed in C++.

// C language to demonstrate register address
#include <stdio.h>
int main(void) {
	register int a =10;
	printf("%d" ,&a);  //address of register Not Allowed
	return 0;
} 
//prog.c:5:2: error: address of register variable ‘a’ requested
//  printf("%d" ,&a);

// cpp program 
#include <iostream>
using namespace std;   
int main() {
    register int a =10;
    cout<<&a;
    return 0;
}
//0x7fff9424aac4  Allowed in CPP language


There is also a chance that two-register variables can reserve the same register and in such cases, they become alias of each other like the reference. However, they are diagnosed with a warning.

We should only declare register variable which is frequently used but don’t declare global variable as register.

Advertisements

static :- static storage class is used to declare a variable whose accessibility is needed till the end of the program. Memory is allocated only once and resides in memory till the end of the program. Their accessibility with locally to block and global static variables can be accessed anywhere in the program.

extern :- extern storage class is used to mark a variable that it has already initialized[memory allocated] somewhere in the program. It can be the same block, different blocks, or different parts of the program. Declaring a variable as an extern, don’t get memory allocated.

//C Example 
extern int a ;          //Memory is not allocated,Only a is declared  
int b;                  //Memory is allocated 
a=10;                   //ERROR, memory is not allocated,since a is not defined.  
extern int d = 0;       //Memory is allocated, d has declaration as well as definition 


//C example 
/**
* File Name :- file.h  
* header file having a variable int z=10;
* Memory for z is allocated via this file
* Now check below another .c file using this header
**/
// .C file
#include<file.h>
extern int z;           //Only declaration of z , Memory is not allocated here 
int main(void)
{
 z = 10;                // No Error because it is defined in file.h
 return 0;
}

Advertisements

extern storage class is always a point of discussion among developers or interviewee.

As an Embedded developer, it is always a concern to effectively use available memory and trade-off between memory as well as processing speed due to limitations.

Advertisements
Advertisements

One comment

Leave a comment