title: Balancing parentheses using stack data type in C
body: <p>I have this stack data structure, in which I store the data in an array. The stack is defined in the struct stack and type defined as Stack. So, I am using this stack data type to check whether in a given string the parentheses are balanced are not. </p>
  
  <p>I am reading in the value using the get line function. But somehow, if I give the total length of read string into my function isBalanced(buffer,strlen(buffer)), I get right answer but with an erroneous output. I get this as the output:
  Stack underflow: there are no elements in the stack
  The expression is balanced</p>
  
  <p>The first line should not be there because in the isBalanced function, I have placed a lot of checks. Even if it manages to pass this condition matching(peek(s),exp[i]), though it should not, it still has to go through  if (isEmpty(s)) return 0; this test condition, which should ensure that program exits and does not give the Stack underflow message,
  Here is my code, please help me find the problem, in earlier implementation. Though I have the right answer now, by passing one less than the total number of characters read by get line function. I guess '\0' is the problem, but I am not sure why is this behavior so weird:</p>
  
  <pre><code>// This is the stack data struct definition
  typedef struct stack{
      int top; //keeps track of top most element in the stack
      int capacity; //keeps track of number of elements that could be stored
                    // in the stack
      int *array;
  } Stack;
  
  
  
  // This is the stackInterface.h file
  #include "stack.h"
  
  //To create a stack
  Stack *createStack(int);
  
  //Queries about the status of stack
  int isEmpty(Stack *);
  int isFull(Stack *);
  
  //Methods to update stack
  void push(Stack *, int);
  int pop(Stack *);
  
  //Method to look at the top element of the stack
  int peek(Stack *);
  
  
  
  // This is the linking file to the main executable file
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;limits.h&gt;
  #include "stack.h"
  
  Stack *createStack(int capacity){
      Stack *s = malloc(sizeof *s); //new stack is created
      s-&gt;top = -1; // initially top position is empty and hence initialised
                  // to -1.
      s-&gt;capacity = capacity;
      s-&gt;array = malloc(sizeof int*capacity);
      // an array with required capacity is created
      return s;  // pointer to newly created stack is returned
  
  }
  
  int isFull(Stack *s){
      return s-&gt;top == s-&gt;capacity - 1;
      //A short-hand of way of writing that if the top is equal to the
      //number of elements in the stack return 1.
  
  }
  
  int isEmpty(Stack *s){
      return s-&gt;top == -1;
      //top value being -1 signifies that there are no elements in the stack
  }
  
  void push(Stack *s, int value){
      if (!isFull(s))
              s-&gt;array[++s-&gt;top] = value;
         //Incrementing the top position and placing the value at that position
      else
              printf("Stack overflow : the stack is at capacity \n");
  }
  
  int pop(Stack *s){
      if (!isEmpty(s))
              return s-&gt;array[s-&gt;top--];
      //returning the value at top position and decrementing the top position
      printf("Stack underflow: there are no elements in the stack\n");
      return INT_MIN; // some sentinel value is returned to signify failure
  }
  
  int peek(Stack *s){
      if (!isEmpty(s))
              return s-&gt;array[s-&gt;top];  
      printf("Stack underflow : there are no elements in the stack\n");
      return INT_MIN;   // some sentinel value is returned to signify failure
  }
  
  
  // This is the main file
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include "stackInterface.h"
  
  int matching(char ch1, char ch2){
    return (ch1=='{'&amp;&amp;ch2=='}')||(ch1=='['&amp;&amp;ch2==']')||(ch1=='('&amp;&amp;ch2==')');
      // both ch1 and ch2 should be of matching types
  
  }
  
  int isBalanced(char exp[], int size){
      int i = 0;
      Stack *s = createStack(size);
      while (i &lt; size){
              if (exp[i] == '{' || exp[i] == '[' || exp[i] == '('){
                      push(s, exp[i]);
              }
              else{
                      if (matching(peek(s),exp[i])){
                              if (isEmpty(s)) return 0;
                              pop(s);
                      }
              }
              i++;
      }
      return isEmpty(s);
  }
  
  int main(){
      size_t buf_size = 32;
      ssize_t characters;
      char *buffer = NULL;
      characters = getline(&amp;buffer, &amp;buf_size, stdin);
      puts(buffer);
  
  
      if (isBalanced(buffer,strlen(buffer)-1))
              printf("The expression is balanced \n");
      else
              printf("The expression is not balanced \n");
      return 0;
  }
  </code></pre>
  
  <p>The output which I get if I remove the -1 form isBalanced function should not      be there at all:
      "Stack underflow: there are no elements in the stack" as the first line. And "The expression is balanced" as the second line. The second line is fine, but looking at the code, one can see that the first line should have not been there, due to a lot of checks there. </p>