{"id":5694,"date":"2023-01-07T00:01:51","date_gmt":"2023-01-07T08:01:51","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5694"},"modified":"2022-12-31T11:05:21","modified_gmt":"2022-12-31T19:05:21","slug":"type-qualifiers-const-and-restrict","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5694","title":{"rendered":"Type Qualifiers: <em>const<\/em> and <em>restrict<\/em>"},"content":{"rendered":"<p>When describing data, the C language offers data types and data qualifiers. The data type is well known to any C programmer, defining the kind of data stored: <em>char<\/em>, <em>int<\/em>, <em>float<\/em>, and so on. The qualifier describes additional aspects of the data, such as how it&#8217;s used or whether the compiler should optimize the data&#8217;s storage.<br \/>\n<!--more--><br \/>\nHere are the data qualifier keywords in the C language:<\/p>\n<p><em>const<br \/>\nrestrict<br \/>\nvolatile<br \/>\n_Atomic<\/em><\/p>\n<p>These differ from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2978\">storage class specifiers<\/a>: <em>auto<\/em>, <em>register<\/em>, <em>static<\/em>, <em>extern<\/em>, and <em>typedef<\/em>. In a way, these declarations tell the compiler how to store the data. The data qualifiers tell the compiler how the data is to be treated and suggest optimizations. My descriptions are general and subject to ire of sophomore computer science students, so I can be accused of over-simplification here.<\/p>\n<p>I cover the <em>const<\/em> keyword in my books and online courses. It ensures that the data declared isn&#8217;t modified in the code. Its scope is within the function where the declaration is made. Because the constant value can&#8217;t be modified, it&#8217;s assigned a value in its declaration:<\/p>\n<p><code>const int full = 100;<\/code><\/p>\n<p>Above, integer <code>full<\/code> is set to 100 and made constant. This variable is used throughout the function, but never altered. If an attempt is made, the compiler shrieks in horror.<\/p>\n<p>The <em>const<\/em> data qualifier differs from the defined constant in scope. The precompiler <code>DEFINE<\/code> directive creates a constant visible throughout the source code. But a defined constant value is replaced as the program is built. On the other hand, a <em>const<\/em> value is assigned storage like any other variable and used that way in the program.<\/p>\n<p>Beyond <em>const<\/em>, my bet is that only a few C programmers use the <em>restrict<\/em>, <em>volatile<\/em>, and <em>_Atomic<\/em> qualifiers. From my research, these specifiers are (no surprise) quite specific in how they modify data and when their use is necessary.<\/p>\n<p>You find the <em>restrict<\/em> specifier used in various function declarations in the <em>man<\/em> pages:<\/p>\n<p><code>int printf(const char * restrict format, ...);<br \/>\nchar *fgets(char * restrict str, int size, FILE * restrict stream);<br \/>\nFILE *fopen(const char * restrict path, const char * restrict mode);<\/code><\/p>\n<p>In each declaration, see how <em>restrict<\/em> is associated with a pointer? In a way, think of a <em>restrict<\/em> specifier as a promise made to the compiler that no other pointer is ever used to access the same data chunk. This <em>restrict<\/em>ion (get it?) also implies that each string passed to a function is unique.<\/p>\n<p>The <em>restrict<\/em> specification is made primarily for compiler optimization. By ensuring that only one pointer references a chunk of data, the compiler is able to reduce overhead when accessing the data. I&#8217;m unsure of the mechanics, and though sample code is available to show <em>restrict<\/em> in action, it&#8217;s difficult to visually demonstrate how this optimization works or how it&#8217;s effective.<\/p>\n<p>Next week I ruminate on the <em>volatile<\/em> keyword.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time to plunge the depths of some seldom-used and obscure C language keywords. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5694\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5694","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5694"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5694\/revisions"}],"predecessor-version":[{"id":5707,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5694\/revisions\/5707"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}