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.
Also See, Sum of Digits in C and C Static Function
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
You can also read about the dynamic arrays in c, Short int in C Programming and Tribonacci Series