C storage class

C Storage class

storage class of c are determined the behavior of variables such as like initial value of variable, life time of variable,scope of variable and memory locations of variable. so there are normally use in c. there are four types.

1) extern
2) static
3) auto
4) register

Extern storage class

help of extern storage class we are capable to utilize global variable to inside of function. In case global variable are defined of anywhere within program.

/*
  Example of extern storage class in c
*/
#include <stdio.h>
int main(){
    /*Using of global variable [num1,num2]*/
  extern int num1,num2;
  printf("\n num1: %d",num1); //num1=5
  printf("\n num2: %d",num2); //num2=10
  printf("\n Sum : %d",num1+num2); //sum : 15
}
/*Global Variables definition [num1,num2]*/
int num1=5,num2=10;

Output

 num1: 5
 num2: 10
 Sum : 15

Note that they are also possible to utilize global variable of another file.



Static Storage class

static is an storage class. they are modified normal variable to special variable. the following properties of static variable.

1) static variable memory are create only one time.
2) scope of static variable are until of entire the program execution..

/*
  Example of static storage class in c
*/
#include <stdio.h>

/*Function Declaration*/
void static_demo();

/* 
  Function definition
  function : static_demo 
*/
void static_demo(){
  /*static integer variable*/
  static int flag=1;

  /*Display flag variable*/
  printf("\n flag is : %d",flag);

  /*increment flag by one*/
  flag++;
}
int main(){
  //Function call first time
  static_demo(); 
  //Function call second time
  static_demo(); 
}

Output:

 flag is : 1
 flag is : 2


Auto storage class

auto is a default storage class of variable they are declared in function and within function block. local variables of function are work on auto storage class.

/*
  Example of auto storage class in c
*/
#include <stdio.h>
int main(){
  /*
    This auto storage variable are
    Access by within this function
  */
  auto int num=34;
  {
    /*Access num*/
    printf("\n %d",num);
    /*auto a*/
    auto int a=24;
    /*use only within the block*/ 
    printf("\n %d",a);
  }
}

Output:

34
 24

in this example two auto storage variable (num) and (a). but its access are different. num is used entire the function and within the block but (a) are only utilized by within the defined block.

Register storage class

Register are storage class. if we are declare register variable the memory is allocated by CPU in register rather than in memory( in this case RAM). so why we are use register. the main reason is register is faster compared to normal variable. C89 and C99 compilers are allowed to use register in any data type in c language.there are normally there are utilized in integers char and pointers. because they are contain limited space. array are should not declare in register because there are an large object.

/* 
  Example of register storage class
*/
/*Header file*/
#include <stdio.h>

int main(){
  //registor variable
  register int i=0;
  /*
   Assume that if this loop  executed thousand
  of instruction then register variable are suitable. 
  */
  for(i;i<5;i++){
    printf("%d\n",i);
  }
  return 0;
}

Output

0
1
2
3
4

Important points 1) register are used inside a function in form of local variable. and also used in formal parameter(parameter of function). But not utilize in global variables.
2)register variable are not have memory address.
3)Note that not declared all local variable in register. because register are has one limit.

© 2019, regularcodes.com, All rights reserved