{"id":2,"date":"2013-04-13T00:13:27","date_gmt":"2013-04-13T00:13:27","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?page_id=2"},"modified":"2016-12-25T08:13:35","modified_gmt":"2016-12-25T16:13:35","slug":"sample-page","status":"publish","type":"page","link":"https:\/\/c-for-dummies.com\/blog\/?page_id=2","title":{"rendered":"Preprocessor Directives"},"content":{"rendered":"<p>I don&#8217;t touch upon preprocessor directives in deep detail in the book. The <code>#include<\/code> and <code>#define<\/code> directives are mentioned, but they&#8217;re only two of many preprocessor directives available in the C Language.<\/p>\n<p><strong>What&#8217;s a Preprocessor Directive?<\/strong><\/p>\n<p>A preprocessor directive is given to the compiler before the C code is compiled. The compiler works through the preprocessor directives first, making substitutions, inclusions, and even decisions before the source code is translated into object code.<\/p>\n<p>Here are the various preprocessor directives:<\/p>\n<p><code>#include<br \/>\n#define<br \/>\n#undef<br \/>\n#if<br \/>\n#endif<br \/>\n#elif<br \/>\n#else<br \/>\n#ifndef<\/code><\/p>\n<p>The place you&#8217;ll see most of these directives used are in the various header files. In fact, I recommend you peruse the header files to explore the possibilities of how these directives are put to work. That&#8217;s because most of your code probably won&#8217;t use these guys.<\/p>\n<p><strong>#include<\/strong><\/p>\n<p>The <code>#include<\/code> directive brings in code from another file, typically a header file. The directive is followed by a <em>filename<\/em>. When the filename is enclosed in angle brackets:<\/p>\n<p><code>#include <stdio.h><\/code><\/p>\n<p>the compiler looks for that file in the <code>\/usr\/include<\/code> (or similar) directory. Otherwise, when the <em>filename<\/em> is enclosed in double quotes:<\/p>\n<p><code>#include \"project.h\"<\/code><\/p>\n<p>the file is looked for in the current directory.<\/p>\n<p><strong>#define<\/strong><\/p>\n<p>In the book, I describe how <code>#define<\/code> is used to create constants and make substitutions. It&#8217;s official definition is &#8220;macro substitution.&#8221; It creates shortcuts. When the compiler encounters this preprocessor directive, it works through the code and makes substations as necessary. For example:<\/p>\n<p><code>#define ROWS 5<\/code><\/p>\n<p>Above, the compiler would search for any instance of <code>ROWS<\/code> and replace it with the value 5.<\/p>\n<p>You can include variables in the macro expansions, which is done for the definition of <code>putchar()<\/code> in the stdio.h header file<\/p>\n<p><code>#define putchar(x) putc(x,stdout)<\/code><\/p>\n<p>Be careful when making such expansions, however, as sometimes the compiler may expand the macro twice.<\/p>\n<p><strong>#undef<\/strong><\/p>\n<p>The <code>#undef<\/code> preprocessor directive un-defines something previously created by the <code>#define<\/code> directive. This is an example of a directive you&#8217;ll often find in a header file, but not necessarily in your code.<\/p>\n<p><strong>#if<br \/>\n#endif<\/strong><\/p>\n<p>The <code>#if<\/code> directive evaluates a condition. When the condition is true, the statements that follow <code>#if<\/code> are executed, up until the <code>#endif<\/code> directive. I&#8217;ve actually used this directive in my code a long, long time ago. I created a constant to determine whether the computer was capable of displaying color text or not:<\/p>\n<p><code>#define COLOR 1<\/code><\/p>\n<p>Later <code>#if<\/code> directives examined that constant to determine whether color information was compiled into the code or not:<\/p>\n<p><code>#if COLOR<br \/>\n\/* color code goes here *\/<br \/>\n#endif<\/code><\/p>\n<p>Of course, I had to manually set the <code>COLOR<\/code> value. That was okay; it allowed me to use one source code file to generate two different versions of the program, one for color systems and one for monochrome.<\/p>\n<p>Remember that the <code>#endif<\/code> directive is required to terminate a block of <code>#if<\/code> statements.<\/p>\n<p><strong>#elif<br \/>\n#else<\/strong><\/p>\n<p>These two directives correspond to <code>else if<\/code> and <code>else<\/code> items in an C language if decision tree. They allow you to construct complex decisions using preprocessor directives.<\/p>\n<p><strong>#ifdef<br \/>\n#ifndef<\/strong><\/p>\n<p>These directives form specific <em>if<\/em> conditions based on whether something is defined or not yet defined. The value of what&#8217;s defined doesn&#8217;t matter, just whether the item has been defined or not. For example:<\/p>\n<p><code>#ifndef BLORFUS<br \/>\n#define BLORFUS 64<br \/>\n#endif <span class=\"comments\">\/* BLORFUS *\/<\/span><\/code><\/p>\n<p>The above example creates the constant <code>BLORFUS<\/code> if it hasn&#8217;t yet been defined. You&#8217;ll see lots of those examples inside the header files, if you choose to peruse them. Read my <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1216\">February 21 2015 Lesson<\/a> for a specific example of how to use these directives.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t touch upon preprocessor directives in deep detail in the book. The #include and #define directives are mentioned, but they&#8217;re only two of many preprocessor directives available in the C Language. What&#8217;s a Preprocessor Directive? A preprocessor directive is &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?page_id=2\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2282,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/pages\/2","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/page"}],"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=2"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/pages\/2\/revisions"}],"predecessor-version":[{"id":1280,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/pages\/2\/revisions\/1280"}],"up":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/pages\/2282"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}