title: how can i know that a stack is empty if i use this method to pop elements?
body: <p>I'm trying to implement a stack in C using singly linked lists, it all works fine as long as the stack is non-empty. Once empty my pop method never detects that its empty but gives some random values. Is there any way so that i can know the stack is empty ? Here is the pop method i'm using along with a sample main program output.</p>
  
  <pre><code>int main(int argc, char const *argv[])
  {
      Node* top;
      push(&amp;top,5);
      printf("Popped Element: %d\n",pop(&amp;top));
      printf("Popped Element: %d\n",pop(&amp;top));
  
      return 0;
  }
  
  int pop(Node** top)
  {
      if(*top == NULL)
      {
          printf("Error: Stack is empty!\n");
          return;
      }
      int temp = (*top)-&gt;iData;
      *top = (*top)-&gt;next;
      return temp;
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>Popped Element: 5
  Popped Element: 1707388
  </code></pre>
  
  <p>EDIT: 
  here is the code for push</p>
  
  <pre><code>void push(Node** top ,int num)
  {
      Node* temp = (Node* )malloc(sizeof(Node));
      temp-&gt;iData = num;
      temp-&gt;next = *top;
      *top = temp;
  }
  </code></pre>