answersLogoWhite

0

Advantages of register variable

Updated: 8/10/2023
User Avatar

Wiki User

15y ago

Best Answer

Register variables are a special case of automatic variables. Automatic variables are allocated storage in the memory of the computer; however, for most computers, accessing data in memory is considerably slower than processing in the CPU. These computers often have small amounts of storage within the CPU itself where data can be stored and accessed quickly. These storage cells are called registers. Normally, the compiler determines what data is to be stored in the registers of the CPU at what times. However, the C language provides the storage class register so that the programmer can ``suggest'' to the compiler that particular automatic variables should be allocated to CPU registers, if possible. Thus, register variables provide a certain control over efficiency of program execution. Variables which are used repeatedly or whose access times are critical, may be declared to be of storage class register. Also these register variables are used in huge projects the tiny program developers are not interested to include these register variables, because the tiny programs never requires more time complete its job. These register variables may be used to store constant values so as to make use of it anywhere in the programs. main{ register float a=0;}

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

16y ago

register - Asks the compiler to devote a processor register to this variable in order to speed the program's execution. The compiler may not comply and the variable looses it contents and identity when the function it which it is defined terminates. - niv

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Registers offer the least possible access time for a value. If you are going to be making a great number of reads or writes to a variable in a short amount of time, it would be very beneficial to use a register for this variable, if possible.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

As all the processing is done in the CPU, the idea is that if a variable is going to be used a lot, if possible it should be kept in one of the CPU registers instead of being transferred between the CPU and RAM.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

It has no meaning in C++11 as it was deprecated. It exists merely because it existed in C. The purpose of the keyword was to hint to the compiler that the named variable will be heavily used and should therefore be placed in a CPU register rather than allocated memory of its own, thus improving efficiency. However, not all hardware supports the concept of registers. Also you cannot take the address of a register variable since registers have no address (they are not part of working memory). If you attempt to take the address, the variable will be forced into working memory, defeating the entire purpose of declaring it a register variable.

Most modern C and C++ compilers can work out for themselves which variables can be placed in registers, so there is no real need to hint to the compiler. Hence it is now deprecated.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

In registers. Or on the stack. It's up to the compiler to decide. (Nowadays we very rarely use the 'register' keyword.)

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Advantages of register variable
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Difference between register variable and automatic variables?

Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable. Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.


What is the default value of register storage class?

Register storage class is a compiler hint that the variable will be often used, and that it should generate code, if it can, to keep the variable's value in a register.


What is the use of register keyword in C?

The register keyword tells the compiler to store the variable onto the CPU register if space on the register is available. The reasoning is that register operations are always faster than memory operations and thus if used correctly, it can speed up an algorithm. However, the register keyword is a somewhat antiquated procedure since for quite a long time the optimizer in modern compilers are smart enough to detect when storing a variable on the register will be advantageous and will do so during optimization. There for, suggesting to the compiler to store a variable on the register can only make things slower if used incorrectly.The register keyword tells the compiler to store the variable onto the CPU register if space on the register is available. The reasoning is that register operations are always faster than memory operations and thus if used correctly, it can speed up an algorithm. However, the register keyword is a somewhat antiquated procedure since for quite a long time the optimizer in modern compilers are smart enough to detect when storing a variable on the register will be advantageous and will do so during optimization. There for, suggesting to the compiler to store a variable on the register can only make things slower if used incorrectly.


What is Advantages of register variables in C?

Faster execution of code.


What are the functions of a storage class?

There are four storage class specifiers in C and C++. These are - 1. auto : The storage specifier auto refers to automatic variable declaration. The life of an automatic variable is the time during which its parent function is running. The scope of an auto variable is the function scope. They can be accessed only from their parent functions. Syntax : auto int a; 2. register : A register variable has all the characteristics of an auto variable. The only difference is that auto variable uses the main memory to store data and register uses the CPU registers. 3. extern : This storage specifier is used to declare a global variable. The life of these variables is the time during which the program runs.

Related questions

What are register variables in c?

A register specifier for a variable is a compiler hint that the variable is used often and should be kept in a CPU register. The compiler will treat the variable as automatic, but it will give it preference for staying in a register between sequence points.


What is class register?

Register storage class is a compiler hint that the variable will be often used, and that it should generate code, if it can, to keep the variable's value in a register.


Where global variable get register?

const


What happens if you try to find address of register variable?

Either you get a compiler error. Or you get a compiler warning, and the variable won't be stored in register.


What is the difference between normal variable and register variable in c?

In C/C++ when we declare a variable; e.g int var; for this variable (i.e. var) memory is being reserved in RAM (i.e out side processor). If we declare variable like that; register int var2; for this variable memory is being reserved in register of CPU (i.e. withing processor) But register variables are discouraged because processor has to work with registers..... Note: strictly speaking, storage class 'register' means: dear compiler, you might optimize this variable into register, as I won't ever request its address. But of course, it's up to you to decide.


Difference between register variable and automatic variables?

Register variables are stored in register of microprocessor/micro-controller. The read/write access to register variable is the fastest because CPU never need any memory BUS operation to access these variable. Auto variable are stored in stack thus access are much slower. Auto variable can be converted to register by using register keyword before it. It has platform specific limitation. Register variable will work only if free registers are available to hold the variable for a function scope. In case of Microprocessor or microcontrollers having very less number of general purpose registers will never take register variable even if we declare it as register.


Which register is used as a local variable?

accumulator


What is the default value of register storage class?

Register storage class is a compiler hint that the variable will be often used, and that it should generate code, if it can, to keep the variable's value in a register.


What are register variables What are the advantage of using register variables?

Asks the compiler to devote a processor register to this variable in order to speed the program's execution. The compiler may not comply and the variable looses it contents and identity when the function it which it is defined terminates.


How is an integer register variable named as exforsys declared?

void myfun (void) { register int exforsys; ... }


What is the use of register keyword in C?

The register keyword tells the compiler to store the variable onto the CPU register if space on the register is available. The reasoning is that register operations are always faster than memory operations and thus if used correctly, it can speed up an algorithm. However, the register keyword is a somewhat antiquated procedure since for quite a long time the optimizer in modern compilers are smart enough to detect when storing a variable on the register will be advantageous and will do so during optimization. There for, suggesting to the compiler to store a variable on the register can only make things slower if used incorrectly.The register keyword tells the compiler to store the variable onto the CPU register if space on the register is available. The reasoning is that register operations are always faster than memory operations and thus if used correctly, it can speed up an algorithm. However, the register keyword is a somewhat antiquated procedure since for quite a long time the optimizer in modern compilers are smart enough to detect when storing a variable on the register will be advantageous and will do so during optimization. There for, suggesting to the compiler to store a variable on the register can only make things slower if used incorrectly.


What is a register variable in c programming?

A register variable is a variable that we wish to allocate in a CPU register rather than in a memory address (RAM). Register variables must be less than or equal in length to the word length of a CPU register which typically limits their use to integral types and pointers. Typical uses for register variables include counters, accumulators and other regularly-accessed variables within a localised code block. Storage classes were originally inherited from B, however only the static and extern storage classes have any meaningful purpose in C. The register storage class is largely redundant while the auto storage class is wholly redundant. Explicitly declaring a register variable is no guarantee the variable will actually be allocated to a register, it is merely a hint to the compiler that the variable is a candidate. However, modern compilers are perfectly capable of determining which variables make good candidates without hints, so there is typically no need to explicitly declare them. If we have more good candidates than there are registers available, explicitly declaring our preferred candidates might be considered worthwhile, however it's generally better to just let the compiler decide. Note that register variables have no memory address. If we take the address of any variable using the unary '&' operator, then that variable can no longer be considered as a register variable candidate by the compiler. If we explicitly declare a register variable and subsequently attempt to take its address, the compiler will issue a warning, yet another reason to simply let the compiler decide.