{"id":6966,"date":"2025-05-17T00:01:23","date_gmt":"2025-05-17T07:01:23","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6966"},"modified":"2025-04-26T13:34:58","modified_gmt":"2025-04-26T20:34:58","slug":"to-include-or-not-to-include","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6966","title":{"rendered":"To #include or not to #include"},"content":{"rendered":"<p>I had a reader offer me a puzzle the other day. His code ran well without the <code>#include<\/code> directive and he wondered why. I did, too.<br \/>\n<!--more--><br \/>\nMany beginning C programmers are confused regarding the <code>#include<\/code> preprocessor directives. Some believe that these lines add the code that makes the program run, which may be true in other languages but not in C.<\/p>\n<p>An <code>#include<\/code> directive inserts the contents of the named header file into the source code. While these files contain prototypes, definitions, macros, and so on, in C they typically don&#8217;t contain programming code. Yes, a macro is code. But header files in C don&#8217;t contain C language statements, which is popular in C++. (You can write C code in a header file, no problem, but doing so tells me that you are trained in C++.)<\/p>\n<p>In C, it&#8217;s the library files contain the actual code, helping build the object files and are eventually linked to form a program.<\/p>\n<p>What the header files do is to save time. Because the compiler desires to have every function prototyped, the header file saves you from knowing (or having to look up) each function&#8217;s prototype. Minus the prototype, the compiler guesses, which is what happens with this code the reader sent me:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05_17-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_05_17-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    puts(\"How do I work?\");\r\n\r\n    return 0;\r\n}<\/pre>\n<p>When building this code, the following warning is generated:<\/p>\n<p><code>0517b.c:3:2: warning: implicit declaration of function 'puts' is invalid in C99<\/code><\/p>\n<p>The code builds. It runs:<\/p>\n<p><code>How do I work?<\/code><\/p>\n<p>The reader wanted to know why the program runs without the <code>#include <stdio.h><\/code> directive. The answer is that the compiler assumes that the first use of a function is its declaration. Further, the assumption is made that the function returns an integer.<\/p>\n<p>In the case of <em>puts()<\/em>, the compiler sees that the function contains a string, therefore it assumes its definition includes a string as its argument. The return value is an <em>int<\/em>, which is happenstance.<\/p>\n<p>It&#8217;s pure luck that the program runs properly. If you used instead:<\/p>\n<p><code>puts(2);<\/code><\/p>\n<p>The same warning appears. The compiler assumes that <em>puts()<\/em> requires an integer as its argument. The program builds. When it runs, however, it suffers from a segmentation fault. Integer value 2 is not a string.<\/p>\n<p>Consider this modification:<\/p>\n<p><code>puts(\"How do I work?\",4.5);<\/code><\/p>\n<p>The same implicit declaration warning appears when this statement is compiled. The program builds &mdash; and it runs with the same output. That&#8217;s because the computer just ignores the extra argument on the stack. Consider it luck.<\/p>\n<p>If, on the other hand, you include the <code>stdio.h<\/code> header file and then add a bonus argument to the <em>puts()<\/em> function, you see an error. The function&#8217;s use doesn&#8217;t match its prototype declared in the <code>stdio.h<\/code> header file.<\/p>\n<p>So how does the program work without including the header file? Because the linker doesn&#8217;t care what&#8217;s passed to the function. The effect is trivial for <em>puts()<\/em>, but it can be devastating for other functions.<\/p>\n<p>Proper C language procedure requires that you prototype functions, which is the point of including a header file. You can always get around this requirement by setting your own prototype:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_05_17-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_05_17-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\nint puts(const char *s);\r\n\r\nint main()\r\n{\r\n    puts(\"How do I work?\");\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code builds without warnings or errors. The function prototype is supplied without the need to include the header file. Here, if you add an extra parameter to the <em>puts()<\/em> function now, the compiler throws an error as the function doesn&#8217;t match the prototype.<\/p>\n<p>Messing around with this stuff is fun, yet the point is to use the header files. Sure, you can get by without them, but the risk is that you write unstable and non-portable code. It&#8217;s best to follow the proper way of doing things, despite the neat-o aspect of bending the rules and getting away with it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C&#8217;mon, are those <code>#include<\/code> directives really necessary? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6966\">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-6966","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\/6966","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=6966"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6966\/revisions"}],"predecessor-version":[{"id":6978,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6966\/revisions\/6978"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}