Friday, July 29, 2016

C Basics - Register Storage Class in C

Keyword register
Storage CPU registers
Default Initial Value An unpredictable value, which is often called a garbage 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.


int main( ) 
{ 
    register int i ; 
    for ( i = 1 ; i <= 10 ; i++ ) 
    printf ( "\n%d", i ) ; 
    return 0;
}

Use register storage class for frequently accessing variables because accessing register is faster than memory. A good example of frequently used variables is loop counters. The number of general purpose registers in a CPU is limited so we cannot say for sure that the value of register storage class variable would be stored in a register. In such an event the variable works as if its storage class is auto.

If the CPU has 16-bit registers then they cannot hold a float value or a double value, which require 4 and 8 bytes respectively. In such an event the variable works as if its storage class is auto. This also means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location).



Related topics:
Overview of Storage Class in C   |   Auto Storage Class in C   |   Static Storage Class in C   |   Extern Storage Class in C   |   Summary of Storage Class in C

List of topics: C Programming

No comments:

Post a Comment