Header Ads

Storage classes in C


Storage
class specifiers in C language tells to the compiler where to store a variable (Storage area of variable), how to store the variable, Scope of variable, Default value of a variable (if it is not initialized it), what is the initial value of the variable and life time of the variable.

Storage classes of C will provide following information to compiler.

  • Storage area of variable.
  • Scope of variable that is in which block the variable is visible.
  • Life time of a variable that is how long the variable will be there in active mode.
  • Default value of a variable if it is not initialized it
Type of Storage Class: Storage classes in mainly divided into four types:
  • auto
  • extern
  • static
  • register
Properties of All storage class 

properties of storage class

1. auto Storage Class

The auto storage class is the default storage class for all local variables. The scope auto variable is within the function. It is equivalent to local variable.

Syntax

{
        int roll;
        auto int roll;
}

In above example define two variables with same storage class auto and their scope is within the function.

Example of auto storage class

Code

#include<stdio.h> 
#include<conio.h> 
void increment(); 
void main() 
{
  increment(); 
  increment(); 
  increment(); 
  increment(); 
  getch(); 
} 

void increment() 
{ 
  auto int i = 0; 
  printf("%d", i): 
  i++; 
} 

Output

0000 

2. static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope

Example of static storage class

Code

#include<stdio.h> 
#include<conio.h> 
void increment(); 
void main() 
{ 
  increment(); 
  increment(); 
  increment(); 
  increment(); 
  getch(); 
}

void increment() 
{ 
  static int i = 0; 
  printf("%d", i);
  i++; 
}

Output

0123

3. extern Storage Class

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. It is equivalent to global variable.

Example of extern storage class

Code

#include<stdio.h> 
#include<conio.h> 
int x = 20; 
void main() 
{ 
  extern int y; 
  printf("The value of x is %d\n",x); 
  printf("The value of y is %d",y); 
  getch(); 
} 
int y=30;

Output

The value of x is 20
The value of y is 30

4. Register variable

Register variables are also local variables, but stored in register memory. Whereas, auto variables are stored in main CPU memory. The register variables are faster than remaining variables, because register variable are stored in register memory not in main memory.

Example

Code

void main() 
{
  register int a=10; 
  ++a; 
  printf("\n value of a: %d",a); 
  printf("Enter a value:"); 
  scanf("%d",&a); 
  --a; 
  printf("\n value of a: %d".a); 
  getch(); 
}

Output

Input data is 50.
Error, must take address of a memory location.

Explanation
  • In scanf() function if address is provided for the register variable then it will give error, if addition is not provided it normally work.
  • Register storage class specifier just recommended to the compiler to hold the variable in CPU register if the memory is available or else stored in stack area of data segment.
  • Limitation: But, only limited variables can be used as register since register size is very low.
  • (16 bits, 32bits, or 64 bits).
  • In TC-3.0 we can't access the address of register variables.


No comments

Powered by Blogger.