top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

When to use register keyword in C ? Can anyone explain with example?

+2 votes
411 views
When to use register keyword in C ? Can anyone explain with example?
posted Jul 21, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Registers are faster than memory to access, so most frequently variables can be put in registers using register keyword. The keyword register hints to compiler that a given variable can be put in a register (It’s compiler’s choice to put or not).

Things to remember with register variable -
1) If you use & operator with a register variable then compiler may give an error or warning (Obviously if variable is stored in a register instead of memory and accessing address of a register is invalid).
2) There is no limit on number of register variables in a C program but it does not mean all will be put in register (compiler may put some variables in register and some not)
3) register keyword can be used with pointer variables. Obviously, a register can have address of a memory location.
4) Register is a storage class and register can not be used with static storage class.

answer Jul 21, 2015 by Salil Agrawal
...