Checking for parentheses balance in C A recent assignment asks me to create a program that checks for parentheses balance in a given string. As I can't find any duplicate questions that have code written in C, I decided to post another one.

I tried to make a stack that stores currently opened / unclosed parentheses and checks with each character in array, if it is unbalanced the program flags and depending on the flag boolean prints different value. 

Is this the best way? Are there any situations where this program will print incorrect outputs? How can I make it take less time if the input is extremely long?

This is the code:

#include&lt;stdio.h&gt;
int main(){
    int pilepointer=-1,flag=0,i;
    char line[10001],pile[10001];
    gets(line);
    for(i=0;line[i]!=NULL;i++){
        switch(line[i]){
        case '(':
            pilepointer=pilepointer+1;
            pile[pilepointer]='(';
            break;
        case '[':
            pilepointer=pilepointer+1;
            pile[pilepointer]='[';
            break;
        case '{':
            pilepointer=pilepointer+1;
            pile[pilepointer]='{';
            break;
        case ')':
            if(pile[pilepointer]=='('){
                pile[pilepointer]==NULL;
                pilepointer=pilepointer-1;
                break;
            }
            else{
                flag=1;
                break;
            }
        case ']':
            if(pile[pilepointer]=='['){
                pile[pilepointer]==NULL;
                pilepointer=pilepointer-1;
                break;
            }
            else{
                flag=1;
                break;
            }
        case '}':
            if(pile[pilepointer]=='{'){
                pile[pilepointer]==NULL;
                pilepointer=pilepointer-1;
                break;
            }
            else{
                flag=1;
                break;
            }
        }
    }
    if(flag==1){
        printf("No\n");
    }
    else{
        printf("Yes\n");
    }
    return 0;
}
Bugs


The loop header is wrong:


for(i=0;line[i]!=NULL;i++){



Here, you want to scan until you find a byte with ASCII value 0 (also called an ASCII NUL, and written in C as '\0').  However, you wrote NULL, which is supposed to represent a null pointer (a pointer to memory address 0).

In practice, you might get away with confounding the two, since they are both zero-like in nature, but it is not semantically correct.
The result of this comparison is thrown away:


pile[pilepointer]==NULL;



Did you intend to write pile[pilepointer] = NULL?  That wouldn't be quite right either, since you've confused NULL and '\0' again, as above.  In any case, that statement was unnecessary.
Stack underflow is possible.  If the input is simply ")", for example, you will immediately try to peek at the top of an empty stack (pile[-1]), resulting in out-of-bounds memory access.


Style


Flag variables suck; flag variables named flag are even worse.  At the least, rename it to something meaningful, such as mismatch_found.
When you do


flag = 1;
break;



You are breaking out of the case.  However, you need not bother analyzing the rest of the line.
Pushing and popping are clumsily expressed.  I would write:

char pile[10001];
int pile_size = 0;


To push:

pile[pile_size++] = '(';


To pop:

if (!pile_size || pile[--pile_size] != '(') {
    // Mismatch detected
}

You can coalesce all of the popping cases, if you push the expected matching closing delimiter when you encounter each opening delimiter.

for (char *c = line; *c &amp;&amp; !mismatch_found; c++) {
    switch(*c) {
    case '(':
        pile[pile_size++] = ')';
        break;
    case '[':
        pile[pile_size++] = ']';
        break;
    case '{':
        pile[pile_size++] = '}';
        break;
    case ')':
    case ']':
    case '}':
        if (!pile_size || pile[--pile_size] != *c) {
            mismatch_found = 1;
        }
        break;
    }
}
printf((pile_size == 0 &amp;&amp; !mismatch_found) ? "Yes\n" : "No\n");