Restrict keyword in C


Here we will see what is the restrict keyword in C. The restrict keyword first introduced in C99 version. Let us see what is actually this restrict keyword.

  • The restrict keyword is used for pointer declarations as a type quantifier of the pointer.

  • This keyword does not add new functionalities. Using this the programmer can inform about an optimization that compiler can make.

  • When the restrict keyword is used with a pointer p, then it tells the compiler, that ptr is only way to access the object pointed by this. So compiler will not add any additional checks.

  • If the programmer uses restrict keyword then violate the above condition, it will generate some un-defined behavior.

Example

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

Output

40 50 30

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements