{"id":2768,"date":"2024-05-10T11:24:28","date_gmt":"2024-05-10T11:24:28","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2768"},"modified":"2024-05-10T11:24:28","modified_gmt":"2024-05-10T11:24:28","slug":"restrict-keyword-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/restrict-keyword-in-c\/","title":{"rendered":"Restrict Keyword In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#restrict-keyword-in-c\">Restrict Keyword In C<\/a><\/li><li ><a href=\"#example-of-restrict\">Example of restrict <\/a><\/li><li ><a href=\"#output\">Output<\/a><\/li><li ><a href=\"#faq-restrict-keyword-in-c\">FAQ-Restrict Keyword In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"restrict-keyword-in-c\">Restrict Keyword In C<\/h2>\n\n\n\n<p>In the C programming language, there&#8217;s a keyword called &#8220;restrict&#8221; introduced after the C99 standard. It&#8217;s used to help the compiler optimize code. When you use &#8220;restrict&#8221; with a pointer, it tells the compiler that this pointer is the only way to access the data it&#8217;s pointing to, with no other pointers pointing to the same data. This information allows the compiler to make code faster. If you use &#8220;restrict&#8221; and don&#8217;t follow this rule, the results could be unpredictable.<\/p>\n\n\n\n<p>One important thing to know is that &#8220;restrict&#8221; is only for C, and you can&#8217;t use it in C++.<\/p>\n\n\n\n<p>In essence, &#8220;restrict&#8221; helps the compiler make code faster by letting it know that a specific pointer is the only one accessing certain data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-of-restrict\">Example of restrict <\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to use restrict keyword.\n#include &lt;stdio.h&gt;\n \n\/\/ Note that the purpose of restrict is to\n\/\/ show only syntax. It doesn't change anything\n\/\/ in output (or logic). It is just a way for\n\/\/ programmer to tell compiler about an\n\/\/ optimization\nvoid use(int* a, int* b, int* restrict c)\n{\n    *a += *c;\n \n    \/\/ Since c is restrict, compiler will\n    \/\/ not reload value at address c in\n    \/\/ its assembly code. Therefore generated\n    \/\/ assembly code is optimized\n    *b += *c;\n}\n \nint main(void)\n{\n    int a = 50, b = 60, c = 70;\n    use(&amp;a, &amp;b, &amp;c);\n    printf(\"%d %d %d\", a, b, c);\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"output\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>120 130 70<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-restrict-keyword-in-c\">FAQ-Restrict Keyword In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1697611315181\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is a restrict qualifier?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The &#8220;restrict&#8221; qualifier in C is used with pointers to indicate that a specific pointer is the primary or sole means of accessing a particular object, and there should be no other pointers pointing to that same object. This informs the compiler to potentially optimize code accordingly by avoiding aliasing. It can improve code performance but must be used carefully to prevent undefined behavior. &#8220;Restrict&#8221; is a feature in C, not in C++.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697611327831\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is char * restrict in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, &#8220;restrict&#8221; is considered a &#8220;type qualifier,&#8221; and it falls into the same category as &#8220;const&#8221; and &#8220;volatile.&#8221; Type qualifiers modify the behavior of data types. When using &#8220;restrict,&#8221; it&#8217;s applied to pointer types, which means you can write something like &#8220;T * restrict&#8221; to create a new type. For instance, &#8220;char const * restrict&#8221; is a valid type. &#8220;Restrict&#8221; is a way to indicate that certain pointers are the primary or sole means of accessing data, which can help the compiler optimize code. It&#8217;s an important tool in C for improving performance while working with pointers.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697611338671\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is restrict keyword in C inline?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The &#8220;restrict&#8221; keyword in C is indeed a type qualifier that can be used with pointer variables. You can apply it to any declaration of a pointer variable or a function parameter. To use &#8220;restrict,&#8221; you insert it after the asterisk (*) and before the variable name in the pointer declaration. This keyword indicates to the compiler that the pointer is the primary or sole means of accessing a specific object, which can lead to potential performance optimizations by the compiler.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Restrict Keyword In C In the C programming language, there&#8217;s a keyword called &#8220;restrict&#8221; introduced after the C99 standard. It&#8217;s used to help the compiler optimize code. When you use &#8220;restrict&#8221; with a pointer, it tells the compiler that this pointer is the only way to access the data it&#8217;s pointing to, with no other &#8230; <a title=\"Restrict Keyword In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/restrict-keyword-in-c\/\" aria-label=\"More on Restrict Keyword In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5383,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[477],"class_list":["post-2768","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-restrict-keyword-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2768"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2768\/revisions"}],"predecessor-version":[{"id":10732,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2768\/revisions\/10732"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5383"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}