Thursday, February 2, 2012

Register Storage Class

A value stored in a CPU register can always be accessed faster than the one that is stored in memory. Therefore, if a variable is used at many places in a program, it is better to declare its storage class as register.
The features of a register storage variable as following manner:
Storage CPU registers
Keyword register
Default initial value Garbage value i.e. an unpredictable value
Scope Local to the block, in which the variable is defined
Life Till the control remains within the block in which the variable is defined


There are no guaranty that we have declare any variable as register and it would be stored in CPU register! Why? The reason is because CPU register are limited, and they may be busy doing some other task. In this case that variable works as default storage class i.e. automatic storage class.
Note: Any variable stored in CPU register or not depend on capacity of microprocessor. For example, if the microprocessor has 16-bit register then they cannot hold a float value or a double value, which require 4 and 8 bytes respectively.
However, if you use the register storage class for float, double variable then you won't get any error messages because compiler treat it as default storage class i.e. auto storage class.

All looping program where a variable are frequently used, declare variable as register.


/*shows uses of register storage class*/
#include<stdio.h>
int main()
{
 register int i;
 for(i=10; i>=0; i=i-2)
    printf("%d ",i);
 return 0;
}


OUTPUT:-


10 8 6 4 2 0


You might also like to read:

  1. What is storage classes in C
  2. Automatic storage class
  3. External storage class
  4. Static storage class
  5. Comparison of storage classes

No comments:

Post a Comment