{"id":3240,"date":"2018-08-18T00:01:53","date_gmt":"2018-08-18T07:01:53","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3240"},"modified":"2018-08-11T12:53:19","modified_gmt":"2018-08-11T19:53:19","slug":"what-evil-lurks-in-a-header-file","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3240","title":{"rendered":"What Evil Lurks in a Header File . . . ?"},"content":{"rendered":"<p>As a beginner, your first exposure to a header file is most likely <code>&lt;stdio.h&gt;<\/code>, which you understand is necessary for some reason. Then you may confuse the header file with the library, which is common but wrong. And you may even dabble with your own header files. Beyond that, few tutorials bother to mention any necessary details about what should go into a header file and when it&#8217;s necessary . . . until now.<br \/>\n<!--more--><br \/>\nThe header file is tied to the preprocessor directive <code>#include<\/code>. It&#8217;s function is to insert another file inside your source code file. You can include any file, even another source code file, if you like. Traditionally, header files are included:<\/p>\n<p><code>#include &lt;stdio.h&gt;<\/code><\/p>\n<p>The angle brackets, &lt; and &gt;, direct the compiler to look for the header file in the standard locations, such as <code>\/usr\/include<\/code> and <code>\/usr\/local\/include<\/code> on Unix systems and <em>god-knows-where<\/em> in Windows. When double quotes are used, the compiler looks in the current directory or the specified pathname:<\/p>\n<p><code>#include \"myheader.h\"<\/code><\/p>\n<p>The compiler may also have flags directing it to look in a specific location:<\/p>\n<p><code>-I~\/include\/curl\/<\/code><\/p>\n<p>Above, the <code>\/include\/curl\/<\/code> directory in the user&#8217;s home folder is scanned for header files. And the <code>CFLAGS<\/code> environment variable can be set to specify compiling options, which includes the &#8220;include&#8221; directory.<\/p>\n<p>As far as contents go, you can put anything in a header file that normally goes into a source code file. No rules exist, though it&#8217;s customary for a header file to include the following:<\/p>\n<ul>\n<li>Other header files<\/li>\n<li>Function prototypes<\/li>\n<li>Constants<\/li>\n<li>Typedefs<\/li>\n<li>Structure definitions<\/li>\n<li>Macros<\/li>\n<\/ul>\n<p>Header files come into play for standard library functions, as they provide the function prototypes, structures, macros, typedefs, and other items that you must otherwise write in your source code. Beyond that, the only time I use a header file is for a multi-module program.<\/p>\n<p>For example, when I&#8217;m coding some complex utility or game, I put all the function prototypes, structure definitions, and constants within the header file. I also place into that header file any other header files required by the code, such as <code>stdio.h<\/code>, <code>stdlib.h<\/code>, and so on. This collection allows the source code files to remain rather brief. Each one might just have the following, sole preprocessor directive:<\/p>\n<p><code>#include \"myheader.h\"<\/code><\/p>\n<p>You can also put C code into a header file, which is more popular in the C++ language than in C. No rule restricts you from placing code into a header file, it&#8217;s just not that common. I would offer that doing so obfuscates your main source code file because the functions held in the header file aren&#8217;t obvious to anyone else. I even go so far as not to include macros in my own header files, as I want to see all the gears and levers in one spot, which helps for debugging.<\/p>\n<p>As an aside, when I reach the point in a multi-module program when I need a custom header file is usually the point when I give up on command line programming and use an IDE or editor. It&#8217;s just easier to manage such programming complexities in that environment as opposed to being the purist that I am and coding at the command prompt.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s easy to craft your own header files in C, but when is a custom header file necessary? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3240\">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-3240","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\/3240","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=3240"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3240\/revisions"}],"predecessor-version":[{"id":3249,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3240\/revisions\/3249"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}