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);
  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);
  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);
  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.

Company Wise Coding Practice    Topic Wise Coding Practice

Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here.

  • numid

    If we have declared a variable as register and no free registers are available at that point of time, will the compiler put that variable in register after some time elapses? Also, as the compiler does optimizations for itself (as mentioned here) , suppose variable ‘a’ is frequently used and the compiler puts that variable in register for optimization then if at later point of time the user is trying to access the address of variable ‘a’ ,will that give a segmentation fault? (Though as the user has not explicitly declared the variable register it shouldn’t)

    • NITJ

      Defining ‘register’ goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register – depending on hardware and implimentation restrictions.

  • Mallesh Koujalagi

    For point 3: Say C does not allow Multiple storage,
    register static int *a = &i; /*Wrong*/

    But how it’s possible
    auto register int *a = &i;

    Please clarify on this

    • Apoorva

      Mallesh … What u said is not possiblle… Even if u do so the compiler gives you an error..

  • apoorv

    C compiler does not Allow Address Access for register Variable but C++ Allows-
    using namespace std;
    int main()
    register int i = 0;
    cout << &i;

  • eragon

    3 int main()
    4 {
    5 int a=5;
    6 register int *b= &a;
    8 printf(“%dn”,*b);
    9 printf(“%pn”,b);
    10 return 0;
    11 }


    >> 5
    >> some address

    can some one expain…

    i got error when i tried to do this

    register int i =10;
    int *a = &i;

    so how come the first part is working nd dis is not??

    • eragon

      my bad…above i’m tring to print address of a..not of register var b..

    • Kedar Kulz

      where did you get error ?? the o/p for the code is correct ..!

  • Jagannath Suhit

    Actually there is a limit on the number of register variables and it equals the number of registers present. The only happy thing is compiler doesnt throw error even if the register space is up and stores them in auto class

    • Kantharr

      Is there a way to know how many registers you have? I would love to use this but I would also want to know how many registers I have left before attempting to make register variables.

      • Jagannath Suhit

        I don’t think there is a direct way of knowing the number of registers available at the moment. But to know the total num of registers is not a matter as knowing the machine architecture details would tell u about it. To know the available registers, u must be doing some microprocessor operations.

  • stranger

    how can you say “There is no limit on number of register variables in a C program”, bcoz the no of registers depends on the “microprocessor architecture”????

  • Silent

    this means if the value of a register variable is changed, this change is done in the register directly??

    • Saurabh

      Thats’s because register variable has all the properties of local variable except it is placed in CPU registers.Since local variables have block scope & there is no block if define globally.

    • kaustavju

      Dev-Cpp is not a compiler !

      • Chirag Patel

        but the compiler it is using is gcc and it is not giving error!!

    • jay

      as mentioned above it is a request not a obligation…if it is not giving any error it means it is assigned the memory not the register

      • Roberto

        Sorry but it is not correct. C doesn’t allow take the address of a register variable, but C++ does it. This is the point, if you use a
        C++ compiler for C code some strange things will happen, and this is one of them.

  • abhi

    can anybody define all storage class specifiers in one program?

    /* Paste your code here (You may delete these lines if not writing code) */
  • Jeevarathnam

    This code also not giving any error.

    void main()
    	register r_var=5;
    	register *r_ptr;
    	*r_ptr = 5;
    	printf("%d,%d,%d\n",r_var, *r_ptr, r_ptr);
    • dream_coder

      r_ptr stores the memory location where 5 is stored not the address where r_ptr is stored.

      &r_ptr will point to address of register where register variable r_ptr is stored.

      • Aryan kumar

        just you reply me sir why we cant scan the value of a variable which is written as ‘register int a’, but we can scan the value of a variable which is written as ‘register int *a’,scanf(“%d”,a)….why give me the valid reason.

        /* Paste your code here (You may delete these lines if not writing code) */
        • Radu

          A “register” variable is not stored in the memory but in the core register file and thus the location doesn’t have an address.

          scanf takes the address in the memory where the result should be placed and trying to apply the address operator (&) on a register, will fail with an error.

          However, if you declare a register int *, that means that you have an actual memory address stored in the register file, so scanf will place the result in that memory location.

          I hope I was clear enough.

    • Because you have not used address operator & any where in the program . so this is working fine . You can not get memory address of any register variable .

      and if you are trying to say that you are modifying the value like *r_ptr=5; . so its changing the value of an address (which is a garbage location preveously sored in register )

    • jay

      this is giving error…….

  • Siva Sai

    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 ?

    int main()
      register int i[1000] ;
      i[200] = 3;
      printf("\n this register array testing .. i[200] = %d\n",i[200]);
      return 0;
    • Radu

      i is pointer in this case and I’m thinking that the compiler will place the address i in a register and use it to construct addresses with base + offset when you access i[some_value]

      • Eventhough the register keyword is used, compiler chooses whether to allocate the variable in register or not. So if there is temperory registers available then it will be used otherwise it will be allocated in the stack.

        Why arrays are not supported ?
        According to C definition, arrays are continious regions in the memory. Registors are individual bytes within the processor which is not part of the RAM. So I guess you got the answer.

    • Rakesh

      Hi Siva, I am somewhat new to C. Can you please let me know why are you expecting any error message here.

  • 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

  • Sandeep


    I also read it somewhere that register cannot be used for arrays, but programs like below do not give any error.

     register char arr[100] = "sandeep";
     printf("%s", arr);

    Could you give an example?

    • arjun

      Hi, above program works fine bcoz compiler will remove the register keyword when program will compile. arrays can not be stored in register.

  • 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.