Learn How To Convert Postfix To Infix Notation using Stack in C Programming Language. The Postfix notation is also known as Reverse Polish Notation. Before you proceed further with this code, you must know the complete operations of stack data structure.
Postfix To Infix Conversion Example
Postfix String: 66+
Infix String: 6 + 6
Algorithm To Convert Postfix Expression into Infix Notation
- Scan the Postfix String from Left to Right.
- If the character is an Operand, then Push it on to the Stack.
- If the character is an Operator, then Pop Operator 1 and Operand 2 and concatenate them using Infix notation where the Operator is in between the Two Operands.
- The resultant expression is then pushed on the Stack
- Repeat the above steps till the Postfix string is not scanned completely.
- Use parantheses properly to ensure correct order for evaluating the final expression.
C Program For Postfix To Infix Conversion using Stack Data Structure
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 | #include<stdio.h> #include<stdlib.h> #include<string.h> # define MAX 20 char str[MAX], stack[MAX]; int top = -1; char pop() { return stack[top--]; } void push(char ch) { stack[++top] = ch; } void postfix_to_infix(char expression[]) { int count, length; char element, operator; length = strlen(expression); for(count = 0; count < MAX; count++) { stack[count] = 0; } printf("\nInfix Expression:\t"); printf("%c", expression[0]); for(count = 1; count < length; count++) { if(expression[count] == '-' || expression[count] == '/' || expression[count] == '*'|| expression[count] == '+') { element = pop(); operator = expression[count]; printf(" %c %c", operator, element); } else { push(expression[count]); } } printf("%c", expression[top--]); } int main() { char postfix_expression[50]; printf("\nEnter Postfix Expression:\t"); scanf("%s", postfix_expression); postfix_to_infix(postfix_expression); printf("\n"); return 0; } |
Output

In case you get any compilation errors or any doubts in this C Program For Conversion of Postfix Expression into Infix Expression, let us know about it in the Comment Section below.
I have detected a bug in your code if i input let’s say the expression xy-z-uv+/ the infix that i am getting is x-y-z+v/u which is wrong the correct infix is (x-y-z)/(u+v)