Register Storage Class in C (Keyword)

Register Storage Class

Register Storage Class in C

  • Variables belonging to register storage class are local to the block which they are defined in, and get destroyed on exit from the block.
  • A register declaration is equivalent to an an auto declaration, but hints that the declared variable will be accessed frequently.
  • If a variable is declared register, the unary & address of operator may not be applied to it, explicitly or implicitly.
  • Register variables are also given no initial value by the compiler.

Why do we need Register Storage Class

  • Whenever we declare any variable inside C Program then memory will be randomly allocated at particular memory location.
  • We have to keep track of that memory location. We need to access value at that memory location using ampersand operator i.e (&).
  • If we store same variable in the register memory then we can access that memory location directly without using the Address operator.
  • Register variable will be accessed faster than the normal variable thus increasing the operation and program execution.

Working & Understanding of Program

  • int number1 and number2 belongs to automatic storage class.
  • so they will be stored in memory.
  • register int sum belongs to register storage class, it will be stored in register memory
  • there will not be any change in calculation or working of program.
  • Program will simply add the two numbers and print their sum.
  • if we use sum variable in any other part of program then it will be easily accessible rather then int number1 and number2.
#include <stdio.h>
int main()
{
int number1, number2;
register int sum;
printf("\nEnter the Number 1 : ");
scanf("%d",&number1);
printf("\nEnter the Number 2 : ");
scanf("%d",&number2);
sum = number1 + number2;
printf("\nSum of Numbers : %d",sum);
return(0);
}