
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
Restrict Keyword in C
In C, the restrict keyword is a type qualifier that was introduced with the C99 standard. It is used to notify the compiler that a pointer is the only reference or access point to the memory it points to.
We can use the restrict keyword to allow the compiler to make optimizations in code because it knows that no other pointer will point to the same block of memory.
Note: The restrict keyword tells the compiler to restrict a particular memory block such that it can't be accessed by other pointers.
Declaring a Pointer with Restrict Keyword
Following is the syntax for declaring a pointer with the restrict keyword −
type* restrict name;
Let's see what this syntax means −
- type − The data type of the pointer (e.g., int, char, float).
- restrict − The keyword that marks the pointer as restricted.
- name − The name of the pointer variable.
Let's now move on and use a simple example to understand how the restrict keyword works.
Example: Using the restrict Keyword in a C Program
Here is a simple program to understand how the 'restrict' keyword works. Here, we are just updating the values of two variables, x and y.
#include <stdio.h> // Function with restrict pointers void update(int *restrict x, int *restrict y) { *x = *x + 10; *y = *y + 20; } int main() { int a = 5, b = 10; update(&a, &b); printf("a = %d, b = %d\n", a, b); return 0; }
Following is the output after updating the value of the x and y −
a = 15, b = 30
Why Use the Restrict Keyword?
You must be wondering why we need to use the "restrict" keyword when you can get the same outputs even without using it. We mentioned earlier that it helps the compiler to optimize the code. One would realize the actual benefits of using the "restrict" keyword in real-world applications where the codes can stretch up to thousands of lines.
Here we have listed down some of the noticeable advantages of using the "restrict" keyword in a C program −
- Makes the code run faster by realigning the memory operations, which makes it a useful feature in applications where we need quick real-time response.
- Improves code clarity by indicating that one pointer will not point to the same memory as another, essentially making the code easier to understand and maintain.
- Especially useful in performance-critical applications such as scientific computing, numerical analysis, and graphics programming.
In addition, using the "restrict" keyword in highly effective when working with large data sets, since it optimizes the code execution to a great extent.
Example 1: Updating Two Variables without restrict Keyword
Let's use the above program again and try to understand what happens when we don't use the "restrict" keyword. Will it have any significant impact on the way the program runs?
#include <stdio.h> // Function with restrict pointers void update(int *x, int *y) { *x = *x + 10; *y = *y + 20; } int main() { int a = 5, b = 10; update(&a, &b); printf("a = %d, b = %d\n", a, b); return 0; }
Notice that, whether you use the restrict keyword or not, the program will generate the same output. However, the compiler's behavior changes depending on whether we use the restrict keyword or not. Let's see how.
- Without "restrict" − The compiler must assume that x and y might point to the same memory location. If you call update(&a, &b), then both pointers refer to the same variable. In this case, the operations could overlap, and the compiler cannot safely re-order or optimize the instructions.
- With "restrict" − You are assuring the compiler that x and y will never point to the same memory location, which in turn helps the compiler apply optimizations like reordering updates or parallel execution.
If you still pass the same variable for both x and y while using restrict, the program may behave unexpectedly or produce garbage values.
Example 2: Adding the Elements of Two Arrays
In this C program, we will be adding two array elements and store the result in a third array. Here, observe how we are using the restrict keyword in the add function −
#include <stdio.h> // Function with restrict pointers void add(int *restrict a, int *restrict b, int *restrict r, int n) { for (int i = 0; i < n; i++) r[i] = a[i] + b[i]; } int main() { int a[] = {1, 2, 3, 4, 5}; int b[] = {5, 6, 7, 8, 10}; int r[5]; add(a, b, r, 5); for (int i = 0; i < 5; i++) printf("%d ", r[i]); return 0; }
In this program, the restrict keyword tells the compiler the arrays donât overlap, allowing faster optimization. On executing the above code, we get the following output −
6 8 10 12 15
Conclusion
The restrict keyword in C is a powerful feature that helps the compiler generate faster and more efficient code. It makes sure that a pointer is the only reference to its memory block, allowing safe optimizations such as reordering, vectorization, and parallel execution.