The Wayback Machine - https://web.archive.org/web/20240624201540/https://www.geeksforgeeks.org/understanding-register-keyword/
Open In App

Understanding “register” keyword in C

Last Updated : 05 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 the register keyword.  The keyword register hints to the compiler that a given variable can be put in a register. It’s the compiler’s choice to put it in a register or not. Generally, compilers themselves do optimizations and put the variables in a register.

Following are some interesting facts about the “register” keyword in C:

1. If you use & operator with a register variable then the 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 the address of a register is invalid.

Try the below program:

C
// C program that demonstrates accessing the address of a
// register is invalid
#include <stdio.h>

int main()
{
    // Declaring a register variable 'i' and initializing it
    // with 10
    register int i = 10;
    // Creating a pointer variable 'a' and assigning the
    // address of 'i' to it
    int* a = &i;
    printf("%d", *a);
    getchar();
    return 0;
}


Output

./Solution.c: In function 'main':
./Solution.c:6:5: error: address of register variable 'i' requested
int* a = &i;
^


2. register keyword can be used with pointer variables. Obviously, a register can have the address of a memory location. There would not be any problem with the below program.

C
// C program that demonstrates register keyword can be used
// with pointer variables

#include <stdio.h>

int main()
{
    // Declaring an integer variable 'i' and initializing it
    // with 10
    int i = 10;
    // Declaring a register pointer variable 'a' and
    // assigning the address of 'i' to it
    register int* a = &i;
    printf("%d", *a);
    getchar();
    return 0;
}

Output
10


3. Register is a storage class, and C doesn’t allow multiple storage class specifiers for a variable. So, the register can not be used with static.

Try the below program:

C
// C program that demonstrates register can not be used with
// static

#include <stdio.h>

int main()
{
    // Declaring an integer variable 'i' and initializing it
    // with 10
    int i = 10;
    // ERROR: Attempting to use both register and static
    // storage classes for 'a'
    register static int* a = &i;
    printf("%d", *a);
    getchar();
    return 0;
}


Output

./Solution.c: In function 'main':
./Solution.c:6:5: error: multiple storage classes in declaration specifiers
register static int* a = &i;
^


4. Register can only be used within a block (local), it can not be used in the global scope (outside main).

C
#include <stdio.h>

// error (global scope)
register int x = 10;
int main()
{
    // works (inside a block)
    register int i = 10;
    printf("%d\n", i);
    printf("%d", x);
    return 0;
}


Output

./Solution.c:4:14: error: register name not specified for 'x'
register int x = 10;
^

5. There is no limit on the number of register variables in a C program, but the point is compiler may put some variables in the register and some not.




Previous Article
Next Article

Similar Reads

Difference between Static variables and Register variables in C
static variables Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope. Syntax: static data_type var_name = var_value; register variables Registers are faster than memory to access
2 min read
extern Keyword in C
extern keyword in C applies to C variables (data objects) and C functions. Basically, the extern keyword extends the visibility of the C variables and C functions. That's probably the reason why it was named extern. Though most people probably understand the difference between the "declaration" and the "definition" of a variable or function, for th
7 min read
C++ | Static Keyword | Question 1
Predict the output of following C++ program. #include &lt;iostream&gt; using namespace std; class Test { static int x; public: Test() { x++; } static int getX() {return x;} }; int Test::x = 0; int main() { cout &lt;&lt; Test::getX() &lt;&lt; &quot; &quot;; Test t[5]; cout &lt;&lt; Test::getX(); } (A) 0 0 (B) 5 5 (C) 0 5 (D) Compiler Error Answer: (
1 min read
C++ | Static Keyword | Question 2
#include &lt;iostream&gt; using namespace std; class Player { private: int id; static int next_id; public: int getID() { return id; } Player() { id = next_id++; } }; int Player::next_id = 1; int main() { Player p1; Player p2; Player p3; cout &lt;&lt; p1.getID() &lt;&lt; &quot; &quot;; cout &lt;&lt; p2.getID() &lt;&lt; &quot; &quot;; cout &lt;&lt; p
1 min read
C++ | friend keyword | Question 1
Predict the output of following program. #include &lt;iostream&gt; using namespace std; class A { protected: int x; public: A() {x = 0;} friend void show(); }; class B: public A { public: B() : y (0) {} private: int y; }; void show() { A a; B b; cout &lt;&lt; &quot;The default value of A::x = &quot; &lt;&lt; a.x &lt;&lt; &quot; &quot;; cout &lt;
1 min read
C++ | friend keyword | Question 2
Predict the output the of following program. #include &lt;iostream&gt; using namespace std; class B; class A { int a; public: A():a(0) { } void show(A&amp; x, B&amp; y); }; class B { private: int b; public: B():b(0) { } friend void A::show(A&amp; x, B&amp; y); }; void A::show(A&amp; x, B&amp; y) { x.a = 10; cout &lt;&lt; &quot;A::a=&quot; &lt;&lt;
1 min read
C++ | Static Keyword | Question 4
Predict the output of following C++ program. #include &lt;iostream&gt; using namespace std; class A { private: int x; public: A(int _x) { x = _x; } int get() { return x; } }; class B { static A a; public: static int get() { return a.get(); } }; int main(void) { B b; cout &lt;&lt; b.get(); return 0; } (A) 0 (B) Linker Error: Undefined reference B::a
1 min read
C++ | const keyword | Question 1
Predict the output of following program #include &lt;iostream&gt; using namespace std; int main() { const char* p = &quot;12345&quot;; const char **q = &amp;p; *q = &quot;abcde&quot;; const char *s = ++p; p = &quot;XYZWVU&quot;; cout &lt;&lt; *++s; return 0; } (A) Compiler Error (B) c (C) b (D) Garbage Value Answer: (B) Explanation: Output is ‘c’ c
1 min read
C++ | const keyword | Question 2
In C++, const qualifier can be applied to 1) Member functions of a class 2) Function arguments 3) To a class data member which is declared as static 4) Reference variables (A) Only 1, 2 and 3 (B) Only 1, 2 and 4 (C) All (D) Only 1, 3 and 4 Answer: (C) Explanation: When a function is declared as const, it cannot modify data members of its class. Whe
1 min read
C++ | const keyword | Question 3
Predict the output of following program. #include &lt;iostream&gt; using namespace std; class Point { int x, y; public: Point(int i = 0, int j =0) { x = i; y = j; } int getX() const { return x; } int getY() {return y;} }; int main() { const Point t; cout &lt;&lt; t.getX() &lt;&lt; &quot; &quot;; cout &lt;&lt; t.getY(); return 0; } (A) Garbage Value
1 min read
three90RightbarBannerImg