title: Why doesn't initializing a C++ struct to `= {0}` set all of its members to 0?
body: <p>After doing a ton of testing and writing <a href="https://stackoverflow.com/questions/61240589/how-to-initialize-a-struct-to-0-in-c/61240590#61240590">this answer on how to initialize a struct to zero in C++</a> (note: the downvote on it was <em>before</em> my total rewrite of it), I can't understand why <code>= {0}</code> doesn't set all members of the struct to zero!</p>
  <p>If you do this:</p>
  <pre><code>struct data_t
  {
      int num1 = 100;
      int num2 = -100;
      int num3;
      int num4 = 150;
  };
  
  data_t d3 = {0};
  printf(&quot;d3.num1 = %i\nd3.num2 = %i\nd3.num3 = %i\nd3.num4 = %i\n\n&quot;,
         d3.num1, d3.num2, d3.num3, d3.num4);
  </code></pre>
  <p>...the output is:</p>
  <pre><code>d3.num1 = 0
  d3.num2 = -100
  d3.num3 = 0
  d3.num4 = 150
  </code></pre>
  <p>...although I <em>expected</em> the output to be this:</p>
  <pre><code>d3.num1 = 0
  d3.num2 = 0
  d3.num3 = 0
  d3.num4 = 0
  </code></pre>
  <p>...which means that only the FIRST member was set to zero, and all the rest were set to their defaults.</p>
  <p>I was always under the impression that initializing a struct in any of these 3 ways would zero-initialize it, but obviously I'm wrong!</p>
  <ol>
  <li><code>data_t d{}</code></li>
  <li><code>data_t d = {}</code></li>
  <li><code>data_t d = {0}</code></li>
  </ol>
  <p>My key takeaway from <a href="https://stackoverflow.com/questions/61240589/how-to-initialize-a-struct-to-0-in-c/61240590#61240590">this answer</a> is therefore this:</p>
  <blockquote>
  <h3>The big take-away here is that NONE of these: <code>data_t d{}</code>, <code>data_t d = {}</code>, and <code>data_t d = {0}</code>, actually set all members of a struct to zero!</h3>
  <ol>
  <li><code>data_t d{}</code> sets all values to their defaults defined in the struct.</li>
  <li><code>data_t d = {}</code> also sets all values to their defaults.</li>
  <li>And <code>data_t d = {0}</code> sets only the FIRST value to zero, and all other values to their defaults.</li>
  </ol>
  </blockquote>
  <p>So, why doesn't initializing a C++ struct to <code>= {0}</code> set all of its members to 0?</p>
  <p>Note that my key take-aways above actually contradict this rather official-looking documentation I've been using for years (<a href="https://en.cppreference.com/w/cpp/language/zero_initialization" rel="nofollow noreferrer">https://en.cppreference.com/w/cpp/language/zero_initialization</a>), which says that <code>T t = {} ;</code> and <code>T {} ;</code> are both zero initializers, when in fact, according to my tests and take-away above, they are NOT.</p>
  <h2>References:</h2>
  <ol>
  <li><a href="https://stackoverflow.com/questions/61240589/how-to-initialize-a-struct-to-0-in-c/61240590#61240590">How to initialize a struct to 0 in C++</a></li>
  <li>Update: I was just pointed to this reference too: <a href="https://stackoverflow.com/questions/88957/what-does-0-mean-when-initializing-an-object">What does {0} mean when initializing an object?</a></li>
  </ol>