Update appNew update is available. Click here to update.

Restrict keyword in C

Rushali Patnaik
Last Updated: Jun 30, 2022
EASY

Introduction

In programming, keywords are predefined, reserved words with special meanings to the compiler. In the C programming language, there is a keyword restrict used while declaring pointers. The restrict keyword doesn't change the compiler's functionality but signals the compiler to optimize. Sounds interesting, right? In this article, we will discuss the restrict keyword of the C language in detail with the help of code examples. Let us dive into the topic.

Restrict Keyword in C

Restrict is a keyword in the C programming language introduced in 1999 version C99. The restrict keyword is used while declaring pointers. The restrict keyword doesn’t change the functionality or add new features but signals the compiler to optimize. The restrict keyword is used along with a pointer that tells the compiler that the pointer is the only way to access the object referred by it, i.e., no other pointer points to the same object as pointed by the pointer preceded by restrict keyword. If the code violates any condition set on restrict keyword, the compiler throws an undefined behavior error.

#include <stdio.h>  
void add(int* x, int* y, int* restrict z) {  
   *x = *x+*y+*z;  
   *y = *x+*y+*z;
   *z = *x+*y+*z;
}  
main(void) {  
   int x = 10, y = 20, z = 30;  
   add(&x, &y, &z);  
   printf("%d %d %d", x, y, z);  
}  

Output

60 110 200

Frequently Asked Questions

What is restrict in C?

Restrict is a keyword in the C programming language introduced in 1999 version C99. The restrict keyword is used while declaring pointers. The restrict keyword doesn’t change the functionality or add new features but signals the compiler to optimize. 

What does the restrict keyword do in C++?

There's no such keyword in C++. List of C++ keywords can be found in section 2.11/1 of C++ language standard. 

Does C++ have restrict keyword?

There's no such keyword as restrict in C++. List of C++ keywords can be found in section 2.11/1 of C++ language standard. restrict is a keyword in the C99 version of C language and not in C++.

Conclusion

In this article, we have extensively discussed about the restrict keyword in detail. Having gone through this article, I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here are some similar blogs to redirect: Data types in CPointers in C and Understanding strings in C. We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Here are some courses provided by Coding Ninjas: Basics of C++ with DSACompetitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow.

Happy Learning! 

Was this article helpful ?
0 upvotes