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

Understanding “register” keyword in C

Last Updated : 05 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
C++ | Static Keyword | Question 1
Predict the output of following C++ program. #include <iostream> using namespace std; class Test { static int x; public: Test() { x++; } static int getX() {return x;} }; int Test::x = 0; int main() { cout << Test::getX() << " "; Test t[5]; cout << Test::getX(); } (A) 0 0 (B) 5 5 (C) 0 5 (D) Compiler Error Answer: (
1 min read
C++ | Static Keyword | Question 2
#include <iostream> 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 << p1.getID() << " "; cout << p2.getID() << " "; cout << p
1 min read
C++ | friend keyword | Question 1
Predict the output of following program. #include <iostream> 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 << "The default value of A::x = " << a.x << " "; cout <
1 min read
C++ | friend keyword | Question 2
Predict the output the of following program. #include <iostream> using namespace std; class B; class A { int a; public: A():a(0) { } void show(A& x, B& y); }; class B { private: int b; public: B():b(0) { } friend void A::show(A& x, B& y); }; void A::show(A& x, B& y) { x.a = 10; cout << "A::a=" <<
1 min read
C++ | Static Keyword | Question 4
Predict the output of following C++ program. #include <iostream> 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 << 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 <iostream> using namespace std; int main() { const char* p = "12345"; const char **q = &p; *q = "abcde"; const char *s = ++p; p = "XYZWVU"; cout << *++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 <iostream> 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 << t.getX() << " "; cout << t.getY(); return 0; } (A) Garbage Value
1 min read
C++ | Static Keyword | Question 5
#include<iostream> using namespace std; class Test { private: static int count; public: Test& fun(); }; int Test::count = 0; Test& Test::fun() { Test::count++; cout << Test::count << " "; return *this; } int main() { Test t; t.fun().fun().fun().fun(); return 0; } (A) Compiler Error (B) 4 4 4 4 (C) 1 1 1 1 (D) 1 2
1 min read
C++ | const keyword | Question 5
#include <stdio.h> int main() { const int x; x = 10; printf("%d", x); return 0; } (A) Compiler Error (B) 10 (C) 0 (D) Runtime Error Answer: (A) Explanation: One cannot change the value of 'const' variable except at the time of initialization. Compiler does check this.Quiz of this Question
1 min read
C++ | Static Keyword | Question 6
Output of following C++ program? #include <iostream> class Test { public: void fun(); }; static void Test::fun() { std::cout<<"fun() is static\n"; } int main() { Test::fun(); return 0; } Contributed by Pravasi Meet (A) fun() is static (B) Empty Screen (C) Compiler Error Answer: (C) Explanation: The above program fails in compi
1 min read
C++ | const keyword | Question 5
Output of C++ program? #include <iostream> int const s=9; int main() { std::cout << s; return 0; } Contributed by Pravasi Meet (A) 9 (B) Compiler Error Answer: (A) Explanation: The above program compiles & runs fine. Const keyword can be put after the variable type or before variable type. But most programmers prefer to put const ke
1 min read
_Generics Keyword in C
The _Generic keyword in C is used to implement a generic code that can execute statements based on the type of arguments provided. It can be implemented with C macros to imitate function overloading. The _Generic keyword was first introduced in C11 Standard. Syntax of _Generic in C_Generic( (expression), type_1: statements, type_2: statements, . .
3 min read
Working of Keyword long in C programming
In C, long is a keyword that symbolizes the Long datatype. The long data type is a 64-bit two's complement integer with: Size: 64 bit Value: -263 to 263-1 The output of 64 bit GCC compiler would give the size of long as 8 whereas on 32 bit GCC compiler the size would be 4. So it varies from compiler to compiler. Now the question is what exactly is
3 min read
Use of explicit keyword in C++
Explicit Keyword in C++ is used to mark constructors to not implicitly convert types in C++. It is optional for constructors that take exactly one argument and work on constructors(with a single argument) since those are the only constructors that can be used in typecasting. Let's understand explicit keyword through an example. Predict the output o
3 min read
Function overloading and const keyword
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading
5 min read
C++ mutable keyword
The mutable storage class specifier in C++ (or use of mutable keyword in C++) auto, register, static and extern are the storage class specifiers in C. typedef is also considered as a storage class specifier in C. C++ also supports all these storage class specifiers. In addition to this C++, adds one important storage class specifier whose name is m
4 min read
restrict keyword in C
In the C programming language (after the C99 standard), a new keyword is introduced known as restrict. restrict keyword is mainly used in pointer declarations as a type qualifier for pointers.It doesn't add any new functionality. It is only a way for the programmer to inform about an optimization that the compiler can make.When we use restrict with
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
Understanding "volatile" qualifier in C | Set 1 (Introduction)
In spite of tons of literature on C language, "volatile" keyword is somehow not understood well (even by experienced C programmers). We think that the main reason for this is due to not having real-world use-case of a 'volatile' variable in typical C programs that are written in high level language. Basically, unless you're doing some low level har
5 min read
Understanding nullptr in C++
Consider the following C++ program that shows problem with NULL (need of nullptr) [GFGTABS] CPP // C++ program to demonstrate problem with NULL #include <bits/stdc++.h> using namespace std; // function with integer argument void fun(int N) { cout << "fun(int)"; return;} // Overloaded function with char pointer argument void fu
3 min read
Understanding constexpr Specifier in C++
constexpr is a feature added in C++ 11. The main idea is a performance improvement of programs by doing computations at compile time rather than run time. Note that once a program is compiled and finalized by the developer, it is run multiple times by users. The idea is to spend time in compilation and save time at run time (similar to template met
5 min read
Understanding the AWS Cloud Security Model
The security of the AWS cloud service is based on the principle of shared responsibility. AWS takes care of ensuring the safety of the physical infrastructure and the cloud platform. Cloud customers are required to protect their data, applications, and other assets from the cloud environment. This approach ensures that all operations and resources
10 min read
Understanding "volatile" qualifier in C | Set 2 (Examples)
The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time. The system always reads the curre
6 min read
C Programming Language Tutorial
In this C Tutorial, you’ll learn all C programming basic to advanced concepts like variables, arrays, pointers, strings, loops, etc. This C Programming Tutorial is designed for both beginners as well as experienced professionals, who’re looking to learn and enhance their knowledge of the C programming language. What is C?C is a general-purpose, pro
8 min read
C Programs
To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and more. These C Examples cover a range of questions,
9 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
Since C is a structured language, it has some fixed rules for programming. One of them includes changing the size of an array. An array is a collection of items stored at contiguous memory locations. As can be seen, the length (size) of the array above is 9. But what if there is a requirement to change this length (size)? For example, If there is a
9 min read
Data Types in C
Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc. Each data type requires different amounts of memory and has some specific operations which can be performed over it. The data types in C can be classified as follows: Types Description Data Types
7 min read
C Arrays
Array in C is one of the most used data structures in C programming. It is a simple and fast way of storing multiple values under a single name. In this article, we will study the different aspects of array in C language such as array declaration, definition, initialization, types of arrays, array syntax, advantages and disadvantages, and many more
15+ min read
three90RightbarBannerImg