Learn how to evaluate Postfix expression using Stack in C Programming Language. This C code for postfix evaluation takes in a postfix expression from the user and then evaluates it.
What is Postfix Expression?
In a postfix operation, he operators are placed after the operands. On scanning the expression from left to right, first the operands are recieved and then the operators.
Steps To Evaluate a Postfix Expression
- Scan the characters of the postfix string from left to right one by one.
- If the character is an operand then push it on the stack.
- If the character is an operator then pop two elements from the stack and apply the operator to these two characters. Now, push the result on the stack.
- After all the characters have been scanned, pop the remaining element of the stack and that is the value of the arithmetic postfix expression.
Example
Infix Expression: (3 + 4) * (2 / 2)
Postfix Expression: 34+22/*
Evaluation: 7
C Program To Evaluate Postfix Expression using Stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> void push(long int character); long int postfix_evaluation(); int pop(); int isEmpty(); int top; long int stack[50]; char postfix_expression[50]; int main() { long int evaluated_value; top = -1; printf("\nEnter an Expression in Postfix format:\t"); scanf("%[^\n]s", postfix_expression); printf("\nExpression in Postfix Format: \t%s\n", postfix_expression); evaluated_value = postfix_evaluation(); printf("\nEvaluation of Postfix Expression: \t%ld\n", evaluated_value); return 0; } long int postfix_evaluation() { long int x, y, temp, value; int count; for(count = 0; count < strlen(postfix_expression); count++) { if(postfix_expression[count] <= '9' && postfix_expression[count] >= '0') { push(postfix_expression[count] - '0'); } else { x = pop(); y = pop(); switch(postfix_expression[count]) { case '+': temp = y + x; break; case '-': temp = y - x; break; case '*': temp = y * x; break; case '/': temp = y / x; break; case '%': temp = y % x; break; case '^': temp = pow(y, x); break; default: printf("Invalid"); } push(temp); } } value = pop(); return value; } void push(long int character) { if(top > 50) { printf("Stack Overflow\n"); exit(1); } top = top + 1; stack[top] = character; } int pop() { if(isEmpty()) { printf("Stack is Empty\n"); exit(1); } return(stack[top--]); } int isEmpty() { if(top == -1) { return 1; } else { return 0; } } |
Ouput

If you have any doubts or compilation errors in this c program for evaluation of postfix expression using stack in data structure, let us know about it in the comment section below.
This is awesome. Just what i was looking for. Thanks codingalpha. Others have given first conversion of infix to postfix and then the evaluation. I was looking for direct evaluation of postfix expression. Nice work
Its a very good postfix evaluation program without using the function Isdigit, Though I wonder, Is it
really necessary to use the IsEmpty() function? or can we just put that if checking in pop() function?