# Evaluate Postfix Expression using Stack C Program

## C Program To Evaluate Postfix Expression using Stack

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

**Must Read: C Program For Conversion of Infix To Postfix Expression**

#### C Program To Evaluate Postfix Expression using Stack

#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