title: Dangling Pointer in C
body: <p>I wrote a program in C having dangling pointer.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  int *func(void)
  {
      int num;
      num = 100;
      return &amp;num;
  }
  
  int func1(void)
  {
      int x,y,z;
      scanf("%d %d",&amp;y,&amp;z);
      x=y+z;
      return x;
  }
  
  int main(void)
  {
      int *a = func();
      int b;
      b = func1();
      printf("%d\n",*a);
      return 0;
  }
  </code></pre>
  
  <p>I am getting the <strong>output as 100</strong> even though the pointer is dangling.</p>
  
  <p>I made a single change in the above function <code>func1()</code>. Instead of taking the value of <code>y</code> and <code>z</code> from standard input as in above program, now I am assigning the value during compile time.</p>
  
  <p>I redefined the <code>func1()</code> as follows:</p>
  
  <pre><code>int func1(void)
  {
      int x,y,z;
      y=100;
      z=100;
      x=y+z;
      return x;
  }
  </code></pre>
  
  <p>Now the <strong>output is 200</strong>.</p>
  
  <p>Can somebody please explain me the reason for the above two outputs?</p>