title: It's a program for checking balance of parenthesis in C using a stack but it is not working as expected
body: <p>It's a program for checking balance of parenthesis in C using a stack but it is not working as expected. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  struct node {
      char data;
      struct node *next;
  };
  struct node *top = NULL;   //top stores address of top element of stack  
  
  void push(char data) {   //inserting element
      struct node *temp = (node *)malloc(sizeof(struct node));
      temp-&gt;data = data;
      temp-&gt;next = NULL;
      if (top == NULL) {
          top = temp;
          return;
      }
      temp-&gt;next = top;
      top = temp;
  }
  
  void pop() {  //removing element
      struct node *temp = top;
      if (top == NULL) {
          printf("No element to delete");
          return;
      }
      top = top-&gt;next;
      free(temp);
  }
  
  char Top() { //fn tht return top element of stack
      return top-&gt;data;
  }
  
  int isEmpty() {
      if (top != NULL) {
          return 1;
      } else
          return 0;
  }
  
  int ArePair(char opening, char closing) {
      if (opening == '(' &amp;&amp; closing == ')')
          return 1;
      else if (opening == '{' &amp;&amp; closing == '}')
          return 1;
      else if (opening == '[' &amp;&amp; closing == ']')
          return 1;
      return 0;
  }
  
  int Arebalanced(char exp[]) {
      int i;
      for (i = 0; i &lt; strlen(exp); i++) {
          if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
              push(exp[i]);
          else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']') {
              if (isEmpty() || !ArePair(Top(), exp[i]))
                  return 0;
              else
                  pop();
          }
      }
      return isEmpty() ? 1 : 0;
  }
  
  int main() {  
      int i;
      char a[50];
      printf("Enter expession: ");
      gets(a);
      if (Arebalanced(a)) {
          printf("balanced \n");
      } else
          printf("not balanced\n");
  }
  </code></pre>