register

A register storage class is simillart to auto storage class except memory where register variable will get stored. register variable stores in cpu register.

Syntax

register data_type var_name;

Declearation of register variable

register int data;

Properties of register variable:

  • Scope of an register variable is limited within the block where it has declared.
  • Visiblity/lifetime of an register variable is limited within the block where it has declared.
  • default initial value of register variable is garbage.
  • mostely, a register variable gets cpu insted of memory. if register is not free, then its uses stack.
  • register variable gets memory or CPU at run time.
  • register variable execute faster then other variable because its stored in cpu. while execution, The system doesn't need to do any extra work to bring a variable from memory to the CPU.
  • we can't dereference a register variable since it does not have any memory address.

Best practices:

A CPU has a limited number of registers, so it's the programmer's responsibility to use register variables wisely. frequently used variable should be declear as register.


Side note:

Declaring a register variable does not mean it will get a CPU register; it means requesting the compiler to use a CPU register. A variable gets a CPU register only if it's available; otherwise it will get memory in the stack.




©2023-2024 rculock.com