REGISTER STORAGE CLASS

Register storage class:

The location of the register storage class is memory. The default initial value is garbage value, which is unpredictable.  The variable will be alive until the block in which the variable is defined in under the phase of execution. The variable scope is up to the block in which it is defined.

Example:

#include <stdio.h>

int main()

{  

  register int i=1;

  printf(“%d”,i);

   }

return 0;

}

Output:

1

The scope is same as the auto storage class, works only inside the block in which it is defined.

This keyword is used mostly in the looping controllers etc. because; the accessing of variables stored in the registers is much faster than stored in the memory. The looping controllers need the variables every time after each iteration. So mostly register is used in that scenario.

Example:

for(i=0;i<n;i++)

{
i=i+1;

}

The limitation with the register is with the size. We can only define the data types of the variable basing on the maximum size of the register.

 

Example:

If the size of the register is 16 bits, we cannot declare float, double like data types because, they may have size up to 4 and 8 bytes which is not sufficient. If we declare float and double, this may not any error because, the complier will consider them as the auto storage class.

Even though we declare the variable as the register, it may not necessarily taken the storage class as register, because the number of registers used in CPU are limited. If any of the register in not doing any work, it may take variable as register else it won’t take it.

Contributor's Info

Created:
0Comment