Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
restrict

What is the restrict keyword in C?

Behzad Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Use of restrict keyword

  • The restrict keyword is used to declare pointers as a type qualifier
  • It is the only way to inform the programmer about optimizations. It doesn’t add any new functionality.
  • If we use the keyword restrict with some pointer ptr, then ptr is the only way to access the object
  • If we use the restrict keyword wrong, it’ll be error-prone
  • The restrict keyword is not available in C++

Code

#include <stdio.h>
// In this function we are adding the value of variable z to other variables x and y
void function(int* x, int* y, int* restrict z)
{
*x += *z; // Updating value of variable x by adding the value of variables x and z
*y += *z; // Updating value of variable y by adding the value of variables y and z
}
int main(void)
{
int x = 50 , y = 100 , z = 150 ;
function(&x, &y, &z);
printf("%d %d %d", x, y, z);
}

RELATED TAGS

c
restrict

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring