answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

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

Wiki User

14y ago

In C Programming, a static variable declared outside of any function is global, but local to the current module. A static variable declared within a function retains its value between function calls.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A static variable is declared once at the start of a program and it does not disappear when it goes out of scope. An auto variable is declared within a function or block and disappears when it goes out of scope.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between register variable and automatic variables?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


WAP to show the difference between a variable and static variable?

difference between constant and static variables in java


What are the different storage class in c?

The four storage classes in C are: automatic, static, external and register. Note that storage classes are not classes in the object-oriented programming sense, they simply define the scope (visibility) of a variable.Automatic Variables (auto)All local variables are automatic by default so we seldom see the auto keyword in code. Local variables are variables declared at function scope.Static Variables (static)All global variables are static by default. Global variables are variables declared at file scope (outside of any function). Static variables can also be explicitly declared inside functions to override the default automatic storage class. All static variables, whether global or local, are allocated within the program's data segment (static memory) and do not fall from scope even if declared locally. All static variables are initialised to zero by default.It's best to avoid the use of global variables unless they are declared constant (const) as it can be difficult to keep track of all the places where a global variable is being operated upon (accessed or assigned to). This can lead to data races in multi-threaded applications unless we take steps to synchronise all access and assignment operations upon the variable. For that reason it's best to keep variables as localised as possible, passing arguments into functions whenever we need to cross scopes. Non-constant global variables should really only be considered if they truly represent a global concept within the file in which they are declared.External Variables (extern)External storage can only be applied to a global variable declared outwith file scope. That is, when a global variable is declared in one file, any external file can gain access to that same global variable simply by declaring the same name and type but with external storage. It follows that external variables are also static variables and is the only case where a variable has two storage classes. Note the local static variables (including local constant variables) cannot be declared external, they are local to the function in which they are declared.This is another reason why it is best to avoid using too many global variables. While we can generally keep track of which code can access a global variable at file scope we have no means of limiting access from outwith that file. Again, prefer local variables to global variables whenever possible.Register Variables (register)A register variable is a variable that we wish to allocate to a CPU register rather than in RAM. Register variables must be no larger than the word-length of the machine and should only be used when we explicitly require fast access to the variable, such as loop counters, accumulators and pointer variables. Note that CPU registers have no address (no identity we can refer to) so we cannot use the unary '&' operator to take the address of a register variable. This means we cannot use pointers to refer to them indirectly which, in turn, means we can only pass them to functions by value (not by reference). However, to do so would defeat the purpose of using the register storage class.Given the limited number of registers available, there is no guarantee that a register variable will actually be allocated to a register; the register keyword is merely a hint to the compiler. It should be noted that modern compilers are extremely good at optimising code so there is seldom any need to explicitly declare register variables.


Advantages of register variable?

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;}


What is the difference between auto storage class and register storage class?

register behaves like auto class but auto variables are actually local variables.They are created when the functions are created but they are destroyed when the fns is exited.But register-variables are used to speed up the operations

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 are the automatic variables in sas?

_N_ and _ERROR_ are the automatic variable in sas


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.


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.


What are register variables?

A register variables is that which got space in CPU internal register sets this requst can be granted or rejected by CPU ,by register veriable the processing speed become much faster for that variable.


In oops what is the difference between class variable and variable?

Class Variable is a subset of Variables.


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.


How accessing a pointer an a variable differs?

Pointer-variables are variables, so there is no difference.


WAP to show the difference between a variable and static variable?

difference between constant and static variables in java


How are difference from independent variables?

Constants stays the same independent variables is the variable that is being manipulated


What is the difference between a manipulated and responding variable?

Manipulated variables are also known as independent variables. These are the variable which you change in an investigation. Plotted on the x axis.


What are the different storage class in c?

The four storage classes in C are: automatic, static, external and register. Note that storage classes are not classes in the object-oriented programming sense, they simply define the scope (visibility) of a variable.Automatic Variables (auto)All local variables are automatic by default so we seldom see the auto keyword in code. Local variables are variables declared at function scope.Static Variables (static)All global variables are static by default. Global variables are variables declared at file scope (outside of any function). Static variables can also be explicitly declared inside functions to override the default automatic storage class. All static variables, whether global or local, are allocated within the program's data segment (static memory) and do not fall from scope even if declared locally. All static variables are initialised to zero by default.It's best to avoid the use of global variables unless they are declared constant (const) as it can be difficult to keep track of all the places where a global variable is being operated upon (accessed or assigned to). This can lead to data races in multi-threaded applications unless we take steps to synchronise all access and assignment operations upon the variable. For that reason it's best to keep variables as localised as possible, passing arguments into functions whenever we need to cross scopes. Non-constant global variables should really only be considered if they truly represent a global concept within the file in which they are declared.External Variables (extern)External storage can only be applied to a global variable declared outwith file scope. That is, when a global variable is declared in one file, any external file can gain access to that same global variable simply by declaring the same name and type but with external storage. It follows that external variables are also static variables and is the only case where a variable has two storage classes. Note the local static variables (including local constant variables) cannot be declared external, they are local to the function in which they are declared.This is another reason why it is best to avoid using too many global variables. While we can generally keep track of which code can access a global variable at file scope we have no means of limiting access from outwith that file. Again, prefer local variables to global variables whenever possible.Register Variables (register)A register variable is a variable that we wish to allocate to a CPU register rather than in RAM. Register variables must be no larger than the word-length of the machine and should only be used when we explicitly require fast access to the variable, such as loop counters, accumulators and pointer variables. Note that CPU registers have no address (no identity we can refer to) so we cannot use the unary '&' operator to take the address of a register variable. This means we cannot use pointers to refer to them indirectly which, in turn, means we can only pass them to functions by value (not by reference). However, to do so would defeat the purpose of using the register storage class.Given the limited number of registers available, there is no guarantee that a register variable will actually be allocated to a register; the register keyword is merely a hint to the compiler. It should be noted that modern compilers are extremely good at optimising code so there is seldom any need to explicitly declare register variables.