If you’re studying data structures or preparing for coding interviews, there’s a high chance you’ve come across infix to postfix conversion. It’s a classic problem that helps you understand how stacks work and how operators are evaluated in an expression.
Infix to Postfix Using Stack in C
In this post, we’ll talk about what infix and postfix notations are, why we convert between them, and finally how to write a simple C program to convert infix to postfix using a stack. Let’s go step by step, no rush. 😊
What is Infix and Postfix?
Before jumping into code, let’s first understand what these notations mean.
Infix Notation:
This is what we use in day-to-day math. The operator is between the operands.
Example: A + B
Postfix Notation (a.k.a. Reverse Polish Notation):
Here, the operator comes after the operands.
Example: A B +
Why Convert to Postfix?
Infix notation is easy for humans to read, but it’s not great for computers to evaluate directly. It needs operator precedence and parentheses to understand the order.
Postfix, on the other hand, doesn’t need any parentheses. It’s much easier for computers to evaluate using a stack.
Basic Idea Behind the Conversion
To convert infix to postfix, we use a stack to:
- Push operators while reading the expression.
- Pop them when we encounter an operator of lower or equal precedence.
- Output operands (like variables or numbers) directly.
We also need to take care of parentheses properly.
Operator Precedence (High to Low)
^(Right to left)*and/+and-
C Program to Convert Infix to Postfix Using Stack
Here’s a simple C program that demonstrates the logic:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 100
char stack[SIZE];
int top = -1;
void push(char ch) {
stack[++top] = ch;
}
char pop() {
return stack[top--];
}
char peek() {
return stack[top];
}
int precedence(char op) {
if (op == '^')
return 3;
else if (op == '*' || op == '/')
return 2;
else if (op == '+' || op == '-')
return 1;
else
return 0;
}
int isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}
void infixToPostfix(char* infix, char* postfix) {
int i = 0, j = 0;
char ch;
while ((ch = infix[i++]) != '\0') {
if (isalnum(ch)) {
postfix[j++] = ch;
} else if (ch == '(') {
push(ch);
} else if (ch == ')') {
while (top != -1 && peek() != '(') {
postfix[j++] = pop();
}
pop(); // pop '('
} else if (isOperator(ch)) {
while (top != -1 && precedence(peek()) >= precedence(ch)) {
postfix[j++] = pop();
}
push(ch);
}
}
while (top != -1) {
postfix[j++] = pop();
}
postfix[j] = '\0';
}
int main() {
char infix[SIZE], postfix[SIZE];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
Example Input & Output
Input:
(A+B)*C
Output:
AB+C*
Another one:
Input:
A+(B*C)
Output:
ABC*+
Associativity Rules
^is right-associative+,-,*,/are left-associative
Final Words
Converting infix to postfix using stack in C is a super useful problem when learning data structures. It’s not just about memorizing syntax—it teaches you how to handle operator precedence, stack usage, and expression parsing.
Once you’ve nailed this, you’re already a step closer to understanding how compilers process expressions behind the scenes!
Need help converting postfix back to infix or writing the evaluation part? Just ask away 🙂







