Register Storage Class

Register Storage Class

  • The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU.
  • We can not dereference the register variables, i.e., we can not use &operator for the register variable.
  • The access time of the register variables is faster than the automatic variables.
  • The initial default value of the register local variables is 0.
  • The register keyword is used for the variable which should be stored in the CPU register. However, it is the compiler’s choice whether or not; the variables can be stored in the register.
  • We can store pointers into the register, i.e., a register can store the address of a variable.
  • Static variables can not be stored into the register since we can not use more than one storage specifier for the same variable.

Example 1

  1. #include <stdio.h>
  2. int main()
  3. {
  4. register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0. 
  5. printf(“%d”,a);
  6. }

Output:

0

Example 2

  1. #include <stdio.h>
  2. int main()
  3. {
  4. register int a = 0;
  5. printf(“%u”,&a); // This will give a compile time error since we can not access the address of a register variable. 
  6. }

Output:

main.c:5:5: error: address of register variable ?a? requested
printf("%u",&a);
Share Button

Feedback is important to us.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!