{"id":1043,"date":"2022-08-30T15:11:26","date_gmt":"2022-08-30T15:11:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/what-type-of-behaviour-is-this-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:11:26","modified_gmt":"2022-08-30T15:11:26","slug":"what-type-of-behaviour-is-this-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/what-type-of-behaviour-is-this-collection-of-common-programming-errors\/","title":{"rendered":"What type of behaviour is this?-Collection of common programming errors"},"content":{"rendered":"<p><strong>This question is looking for a standardese quote explicitly explaining why this behavior is wrong.<\/strong><\/p>\n<p>The code below including inside <code>main<\/code> ,<\/p>\n<pre><code>int main()\n{\n #include \n printf(\"hello , world \\n\");\n return 0;\n}\n<\/code><\/pre>\n<p>On <code>gcc -Wall in.c -o in.out<\/code> It successfully compiles and prints <code>hello , world<\/code>.<\/p>\n<p>But on <code>clang in.c -o in.out<\/code> It gives me this error :<\/p>\n<pre><code>\/usr\/include\/stdio.h:353:12: error: implicit declaration of 'fprintf' requires \n\ninclusion of the header \nextern int fprintf (FILE *__restrict __stream,\n           ^\n1 error generated.\n<\/code><\/pre>\n<p>My doubt is what kind of behaviour is this ? Is this undefined behaviour or what ?<\/p>\n<p>Also I am not able to find the documentation related to it.<\/p>\n<p>EDIT : The problem is that I found this code somewhere similar to it but I can&#8217;t post that code exactly so I posted this kind of Demo code.I know the Placing <code>stdio.h<\/code> outside the main.<\/p>\n<ol>\n<li>\n<p>C99, 7.1.2\/4:<\/p>\n<blockquote>\n<p>[&#8230;] If used, a header shall be included outside of any external declaration or definition, and it shall first be included before the first reference to any of the functions or objects it declares, or to any of the types or macros it defines.<\/p>\n<\/blockquote>\n<p>4\/2:<\/p>\n<blockquote>\n<p>If a \u2018\u2018shall\u2019\u2019 or \u2018\u2018shall not\u2019\u2019 requirement that appears outside of a constraint is violated, the behavior is undefined.<\/p>\n<\/blockquote>\n<p>6.9\/4:<\/p>\n<blockquote>\n<p>As discussed in 5.1.1.1, the unit of program text after preprocessing is a translation unit, which consists of a sequence of external declarations. These are described as \u2018\u2018external\u2019\u2019 because they appear outside any function (and hence have file scope).<\/p>\n<\/blockquote>\n<p>So I think this is undefined behavior.<\/p>\n<\/li>\n<li>\n<p>In C++11: 17.6.2.2\/3:<\/p>\n<blockquote>\n<p>A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header.<\/p>\n<\/blockquote>\n<p><code>main()<\/code> is extern, so is not a proper context for include.<\/p>\n<\/li>\n<li>\n<p>Try including the header file outside of the main method. Like this.<\/p>\n<pre><code>#include \nint main()\n{\n printf(\"hello , world \\n\");\n return 0;\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:18:10. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>This question is looking for a standardese quote explicitly explaining why this behavior is wrong. The code below including inside main , int main() { #include printf(&#8220;hello , world \\n&#8221;); return 0; } On gcc -Wall in.c -o in.out It successfully compiles and prints hello , world. But on clang in.c -o in.out It gives [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1043","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1043"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1043\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}