Understanding “register” keyword in C
Registers are faster than memory to access, so the variables which are most frequently used in a C program 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 it in a register or not. Generally, compilers themselves do optimizations and put the variables in register.
1) If you use & operator with a register variable then compiler may give an error or warning (depending upon the compiler you are using), because when we say a variable is a register, it may be stored in a register instead of memory and accessing address of a register is invalid. Try below program.
int main()
{
register int i = 10;
int *a = &i;
printf("%d", *a);
getchar();
return 0;
}
2) register keyword can be used with pointer variables. Obviously, a register can have address of a memory location. There would not be any problem with the below program.
int main()
{
int i = 10;
register int *a = &i;
printf("%d", *a);
getchar();
return 0;
}
3) Register is a storage class, and C doesn’t allow multiple storage class specifiers for a variable. So, register can not be used with static . Try below program.
int main()
{
int i = 10;
register static int *a = &i;
printf("%d", *a);
getchar();
return 0;
}
4) There is no limit on number of register variables in a C program, but the point is compiler may put some variables in register and some not.
Please write comments if you find anything incorrect in the above article or you want to share more information about register keyword.


I am also not getting any error or warning while executing below Sample Program( I used GCC compiler ). Why it is not reporting error , Could you please tell ?
#include<stdio.h> int main() { register int i[1000] ; i[200] = 3; printf("\n this register array testing .. i[200] = %d\n",i[200]); return 0; }It should give an error, I am surprised if it is isn’t, if you are compiling with GCC, make sure you have the warnings turned on (-Wall), if you are using Microsoft C/C++ compiler it won’t honor the register variable request it would just put it in the memory, following the msdn link http://msdn.microsoft.com/en-us/library/ds03af1b(VS.71).aspx
@kiran:
I also read it somewhere that register cannot be used for arrays, but programs like below do not give any error.
main() { register char arr[100] = "sandeep"; printf("%s", arr); getchar(); }Could you give an example?
U r right, you cannot dereference a register,because register doesnot have an address. IMO I did not see register variable in any of the program I maintained so far, I have seen some of the legacy code written for SCO UNIX 5 where register is explicitly used for some heavily used variables like inside the for loop or while loop. As you said compilers are now days smart enough to apply the neat optimization themselves, btw one more point register cannot hold the arrays as well.