Register Storage Class in C (Keyword)
Register Storage Class in C
Variables belonging to register storage class are local to the block in which they are 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, then 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 of 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.
Example:
Run
#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); }
Output
Enter the Number 1 : 49 Enter the Number 2 : 37 Sum of Numbers : 86
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment