{"id":6193,"date":"2024-01-13T00:01:18","date_gmt":"2024-01-13T08:01:18","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6193"},"modified":"2023-12-30T14:22:44","modified_gmt":"2023-12-30T22:22:44","slug":"initializing-arrays-in-c23","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6193","title":{"rendered":"Initializing Arrays in C23"},"content":{"rendered":"<p>You can initialize an array in the C programming languages in three ways: Not at all, only some elements, or all of the elements. Some compilers offer a fourth way, which initializes all elements to the same value. With the C23 standard, yet another way to initialize an array is possible.<br \/>\n<!--more--><br \/>\nTo review, an array is uninitialized when declared like this:<\/p>\n<p><code>int values[20];<\/code><\/p>\n<p>The array <code>values[]<\/code> has storage for 20 integers. These storage locations (elements) are defined, but not assigned values. The values could be anything; your code should never assume that all 20 elements are initialized to zero.<\/p>\n<p>You can define only a few elements in the array, as I <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6143\">wrote about<\/a> recently:<\/p>\n<p><code>int numbers[6] = { [3] = 40, [4] = 50 };<\/code><\/p>\n<p>Above, elements 3 and 4 are set the values 40 and 50, but the other elements remain uninitialized. (This method may be non-standard, implemented only in a few compilers.)<\/p>\n<p>You can define all the elements:<\/p>\n<p><code>char string[] = \"Hello!\";<\/code><\/p>\n<p>Back in 2020, I <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4306\">wrote about<\/a> initializing all elements in an array to zero:<\/p>\n<p><code>int scores[5] = { 0 };<\/code><\/p>\n<p>This construction is non-standard, but it makes you think you can initialize an array to all the same values. Alas, it doesn&#8217;t work that way:<\/p>\n<p><code>int scores[5] = { 1 };<\/code><\/p>\n<p>The above statement initializes element zero to 1, but the other four elements are set to zero. Again, this construction is compiler-specific and not part of the C standard.<\/p>\n<p>What is part of the new C23 standard, however, is the following expression:<\/p>\n<p><code>int a[5] = {};<\/code><\/p>\n<p>This statement declares an integer array, <code>a[]<\/code>, with five elements all initialized to zero. The <code>={}<\/code> expression is called the <em>empty initializer<\/em>. In fact, it may already be implemented on your compiler, but its standard with C23.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_01_13-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_01_13-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a[5] = {};\r\n\r\n    for( int x=0; x&lt;5; x++ )\r\n        printf(\"a[%d] = %d\\n\",x,a[x]);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The code above demonstrates the empty initializer. The expression <code>int a[5] = {}<\/code> assigns zeros to the five elements of <code>a[]<\/code>. A <em>for<\/em> loop outputs these values, which are all zeros.<\/p>\n<p>This code builds under the current version of <em>clang<\/em> without the <code>-std=c2x<\/code> switch. Here is the output:<\/p>\n<p><code>a[0] = 0<br \/>\na[1] = 0<br \/>\na[2] = 0<br \/>\na[3] = 0<br \/>\na[4] = 0<\/code><\/p>\n<p>It&#8217;s too bad that C continues to not initialize variables when they&#8217;re declared. For example, to set all integers to zero and real numbers to 0.0 as well as pointers to NULL. I don&#8217;t know whether such a thing was discussed by the standards committee or even if any negatives are associated. But the addition of the empty initializer as a standard is welcome.<\/p>\n<p>In next week&#8217;s Lesson, I review some of the new language features.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C23 standard brings consistency when it comes to initializing arrays. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6193\">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-6193","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\/6193","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=6193"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6193\/revisions"}],"predecessor-version":[{"id":6208,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6193\/revisions\/6208"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}