{"id":5711,"date":"2023-01-21T00:01:44","date_gmt":"2023-01-21T08:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5711"},"modified":"2023-01-07T11:11:53","modified_gmt":"2023-01-07T19:11:53","slug":"not-every-compiler-likes-your-code","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5711","title":{"rendered":"Not Every Compiler Likes Your Code"},"content":{"rendered":"<p>You would think that the various C compilers deal with C code in the same manner. After all, they adhere to the same C standards, right? This compatibility makes it possible to compile and cleanly build C programs regardless of which compiler you use, right? Well, maybe not.<br \/>\n<!--more--><br \/>\nI recently discovered that I technique I use for my C programs works well in the LLVM <em>clang<\/em> compiler, but really ticks off <em>gcc<\/em> in Linux. Here is the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_01_21-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_01_21-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const int size = 5;\r\n    int data[size] = {\r\n        5, 8, 2, 6, 9\r\n    };\r\n    int x = 0;\r\n\r\n    while( x&lt;size )\r\n        printf(\"%d\\n\",data[x++]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 5, I set variable <code>size<\/code> as a constant to be used throughout the code. It represents the number of elements in the <code>data[]<\/code> array. I could just declare the array without a size value, in which case the compiler figures out the number of elements. But I use the element count as the <em>while<\/em> loop&#8217;s condition at Line 11. This approach is more readable, plus it avoids having a &#8220;magic number&#8221; in the code that other programmers may not readily recognize.<\/p>\n<p>The <em>clang<\/em> compiler has no issues with my using a constant to declare the <code>data[]<\/code> array&#8217;s size.<\/p>\n<p>On the Macintosh, the <em>gcc<\/em> compiler (which is probably aliased to <em>clang<\/em>) has no issues either.<\/p>\n<p>In Linux, however, the <em>gcc<\/em> compiler is hostile toward this approach. It spews out this error:<\/p>\n<p><code>error: variable-sized object may not be initialized<\/code><\/p>\n<p>I use this &#8220;variable-sized object&#8221; initialization in a lot of my code. Imagine how annoyed I was when I was using an alien system with only <em>gcc<\/em> available and this error puked all over my screen. (The error message is followed by a series of warnings, one for each array element.)<\/p>\n<p>My frustration with this &#8220;error&#8221; is not only do other compilers not think it&#8217;s an issue, but that <em>gcc<\/em> also lets you declare an array with a variable size. The key is not to initialize it. So the following statement is okee-doke:<\/p>\n<p><code>int data[size];<\/code><\/p>\n<p>You can then manually assign all the element values.<\/p>\n<p>Some of the feedback I&#8217;ve read on the Internet states that it works, but not on a variable-length array (VLA). But <code>data[]<\/code> isn&#8217;t a VLA. In fact, even if I declare the array <em>const<\/em>, <em>gcc<\/em> gets angry with the same intensity.<\/p>\n<p>I&#8217;m at a loss.<\/p>\n<p>I suppose for compatibility&#8217;s sake, I must stop using a constant to initialize an array. I feel the construction is less readable, especially because the constant (for the array&#8217;s size) is used elsewhere in the function, but if I strive to write code compatible with the major compilers, it&#8217;s a sacrifice I must make.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Imagine my horror at discovering that the <em>clang<\/em> compiler lets me do something that the <em>gcc<\/em> compiler finds offensive. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5711\">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-5711","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\/5711","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=5711"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5711\/revisions"}],"predecessor-version":[{"id":5719,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5711\/revisions\/5719"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}