title: Garbage value coming using arrays in C
body: <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  int i;
  float *calculateProduct(int *,int *,int );
  int main()
  {
  
          int n;
          
          printf(&quot;Enter length of array\n&quot;);
          scanf(&quot;%d&quot;,&amp;n);
          
          int x[100];
          int y[100];
          for(i=0;i&lt;n;i++)
          {
              scanf(&quot;%d&quot;,&amp;x[i]);
          }
          for(i=0;i&lt;n;i++)
          {
              scanf(&quot;%d&quot;,&amp;y[i]);
          }
          
          /*for(i=0;i&lt;n;i++)
          {
              printf(&quot;%d &quot;,x[i]);
          }
          printf(&quot;\n&quot;);
          for(i=0;i&lt;n;i++)
          {
              printf(&quot;%d &quot;,y[i]);
          }
          printf(&quot;\n&quot;);
          */
          int *ptr;
          ptr=calculateProduct( x, y, n);
          for(i=0;i&lt;n;i++)
          printf(&quot;%d &quot;,*(ptr+i));
      
          return 0;
  }
  
  float *calculateProduct(int *x,int *y,int n)
  {
      int z[100];
      for(i=0;i&lt;n;i++)
      {
          z[i]=x[i]*y[i]; 
      }
      
      return z;
  }
  </code></pre>
  <p>In the code above, I want to pass 2 arrays to a function and its size and then want to calculate the product of those 2 arrays all using functions and pointers.</p>
  <p>In my test case, I take 3 as the size of the array and enter the elements. The product array shows the result for 1st two elements correctly but not for the 3rd. However, on debugging and including some <code>printf</code> statements I get my answer correctly (I have commented that in my code).</p>
  <p>How is that possible? How come 2 <code>printf</code> statements are able to remove the junk value?</p>