The Wayback Machine - https://web.archive.org/web/20240630160250/https://www.geeksforgeeks.org/restrict-keyword-c/
Open In App

restrict keyword in C

Last Updated : 22 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 a pointer ptr, it tells the compiler that ptr is the only way to access the object pointed by it, in other words, there’s no other pointer pointing to the same object i.e. restrict keyword specifies that a particular pointer argument does not alias any other and the compiler doesn’t need to add any additional checks.
  • If a programmer uses restrict keyword and violates the above condition, the result is undefined behavior.
  • restrict is not supported by C++. It is a C-only keyword.

Example of restrict

C




// C program to use restrict keyword.
#include <stdio.h>
 
// Note that the purpose of restrict is to
// show only syntax. It doesn't change anything
// in output (or logic). It is just a way for
// programmer to tell compiler about an
// optimization
void use(int* a, int* b, int* restrict c)
{
    *a += *c;
 
    // Since c is restrict, compiler will
    // not reload value at address c in
    // its assembly code. Therefore generated
    // assembly code is optimized
    *b += *c;
}
 
int main(void)
{
    int a = 50, b = 60, c = 70;
    use(&a;, &b;, &c;);
    printf("%d %d %d", a, b, c);
    return 0;
}


Output

120 130 70


Similar Reads

How to Restrict Dynamic Allocation of Objects in C++?
C++ programming language allows both auto(or stack-allocated) and dynamically allocated objects. In Java &amp; C#, all objects must be dynamically allocated using new. C++ supports stack-allocated objects for the reason of runtime efficiency. Stack-based objects are implicitly managed by the C++ compiler. They are destroyed when they go out of scop
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