title: Remove zero entries from an array in C
body: <p>I have an array of values <code>x = {0,0,1,2,3,0,0,7,8}</code> and I want to remove the zero entries using C. </p>
  
  <p><strong>Attempt:</strong></p>
  
  <p>I am attempting to loop through each value in the array and check if the entry is not equal to zero. If this condition is true, then I am attempting to populate a new array with original array value.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main() {
      int x[] = { 0, 0, 1, 2, 3, 0, 0, 7, 8 };
      int i;
      int x_upd[100];
      for (i = 0; i &lt; 9; i++) {
          if (x[i] != 0) {
              x_upd[i] = x[i]; // if true, populate new array with value
          }
      }
      for (i = 0; i &lt; 9; i++) {
          printf(" Peak updated %d\t", x_upd[i]); //
      }
      return 0;
  }
  </code></pre>
  
  <p>The output is not giving me the values <code>{1,2,3,7,8}</code> as desired. Instead, I am getting garbage values at the location where the zeros used to be.</p>
  
  <p>Any advice on what I am doing wrong here? Do I need an else statement?</p>