Infix to Postfix Expression: Easy Step-by-Step Guide for Beginners
May 12, 2026 5 Min Read 55 Views
(Last Updated)
If you have ever written a math expression like 3 + 4 or A * B, you have already written an infix expression without even knowing it. But computers do not evaluate math the same way humans do. They prefer a different format called postfix. Understanding how to convert an infix to postfix expression is one of the most important topics in data structures and is asked in almost every CS exam and coding interview.
This guide explains everything from scratch. By the end, you will be able to convert any infix expression to postfix on your own.
Quick Answer
In an infix expression, the operator is written between two values, like A + B. In a postfix expression, the operator is written after the values, like AB+. To convert infix to postfix, you read the expression from left to right. Operands such as letters or numbers are added directly to the output, while operators are temporarily stored in a stack. Operators with higher priority are placed first, and after scanning the full expression, the remaining operators in the stack are added to the end of the output.
Table of contents
- What Is an Operand and Operator
- What Is an Infix Expression
- What Is a Postfix Expression
- What Is Prefix Expression (Just So You Know)
- What Is a Stack and Why Does It Matter Here
- Operator Precedence and Associativity
- Step-by-Step Algorithm: Infix to Postfix Conversion
- Example 1: Simple Expression Without Parentheses
- Example 2: Expression With Parentheses
- Example 3: Full Expression With Multiple Operators
- How to Evaluate a Postfix Expression
- Quick Reference: Infix vs Postfix
- Where Is Infix to Postfix Used in Real Life
- Tips for Beginners
- 💡 Did You Know?
- Conclusion
- FAQs
- What is the difference between infix and postfix expression?
- Why do we convert infix to postfix?
- What data structure is used to convert infix to postfix?
- What is operator precedence in infix to postfix conversion?
- Is infix to postfix conversion asked in coding interviews?
What Is an Operand and Operator
Before jumping into expressions, you need to know these two terms. They appear everywhere in this topic.
Operand:
An operand is the value that gets operated on. It is the number or variable in a math expression.
- In 3 + 4, the operands are 3 and 4
- In A * B, the operands are A and B
- In (X + Y) – Z, the operands are X, Y, and Z
Operator:
An operator is the symbol that tells you what operation to perform between the operands.
| Operator | Symbol | Example |
| Addition | + | A + B |
| Subtraction | – | A – B |
| Multiplication | * | A * B |
| Division | / | A / B |
| Exponent (power) | ^ | A ^ B |
Simple way to remember: Operands are the values. Operators are the actions performed on those values.
Do check out HCL GUVI’s DSA for Programmers Course if you want to master important concepts like stacks, recursion, algorithms, and problem-solving techniques used in coding interviews and real-world programming. This beginner-friendly program helps you strengthen your Data Structures and Algorithms skills through hands-on practice and guided learning.
What Is an Infix Expression
An infix expression is the normal math notation you have used your whole life. The operator sits between the two values it works on.
Examples of infix expressions:
- A + B
- 3 * 4
- (A + B) * C
- A + B * C – D
This is how humans naturally read and write math. But for computers, infix expressions are tricky because the computer has to figure out which operation to do first.
What Is a Postfix Expression
In a postfix expression, the operator comes after the operands it works on. It is also called Reverse Polish Notation (RPN).
Examples of postfix expressions:
- A B + (means A + B)
- 3 4 * (means 3 * 4)
- A B + C * (means (A + B) * C)
- A B C * + D – (means A + B * C – D)
Why do computers prefer postfix?
- No parentheses are needed at all
- No need to figure out operator precedence while evaluating
- Easy to evaluate using a simple stack
- Compilers and calculators use postfix internally
What Is Prefix Expression (Just So You Know)
While this blog focuses on infix to postfix, it helps to see all three formats side by side:
| Type | Format | Example |
| Infix | Operator between operands | A + B |
| Postfix | Operator after operands | A B + |
| Prefix | Operator before operands | + A B |
Postfix and prefix both remove the need for parentheses. Computers almost always use postfix.
What Is a Stack and Why Does It Matter Here
A stack is a data structure that works like a pile of plates. You can only add or remove from the top.
- Push means adding something on top
- Pop means removing from the top
- LIFO means Last In, First Out (the last thing you added comes out first)
When converting infix to postfix, a stack is used to hold operators temporarily. Operands (numbers or variables) go straight to the output. Operators wait in the stack until the right time to come out.
Operator Precedence and Associativity
Before converting, you need to know two things:
Operator Precedence (which operator goes first):
| Precedence Level | Operators |
| Highest | ^ (exponent) |
| Medium | * (multiply), / (divide) |
| Lowest | + (add), – (subtract) |
Higher precedence means that the operator is evaluated first. So in A + B * C, the multiplication happens before the addition.
Associativity (when two operators have equal precedence):
- Most operators (+ , -, *, /) are left to right. So A + B + C means (A + B) + C.
- The exponent ^ is right to left. So A ^ B ^ C means A ^ (B ^ C).
Step-by-Step Algorithm: Infix to Postfix Conversion
Here is the complete algorithm. Read it once, then see it in action with examples below.
Step 1: Scan the infix expression from left to right, one character at a time.
Step 2: If the character is an operand (a letter or number like A, B, 3, 4), write it directly to the output.
Step 3: If the character is an opening parenthesis (, push it onto the stack.
Step 4: If the character is a closing parenthesis ), pop operators from the stack and write them to output until you see an opening parenthesis. Remove the opening parenthesis from the stack but do not write it to output.
Step 5: If the character is an operator (+, -, *, /, ^):
- Pop and write to output all operators on the stack that have higher or equal precedence than the current operator (use right-to-left rule for ^)
- Then push the current operator onto the stack
Step 6: After scanning the full expression, pop and write all remaining operators from the stack to the output.
Example 1: Simple Expression Without Parentheses
Infix: A + B * C
Let us convert this step by step.
| Step | Character | Action | Stack | Output |
| 1 | A | Operand, write to output | (empty) | A |
| 2 | + | Operator, stack is empty, push | + | A |
| 3 | B | Operand, write to output | + | A B |
| 4 | * | Higher precedence than +, push | + * | A B |
| 5 | C | Operand, write to output | + * | A B C |
| 6 | End | Pop all remaining operators | (empty) | A B C * + |
Result: A B C * +
This means: evaluate B * C first, then add A. That matches BODMAS rules exactly.
Example 2: Expression With Parentheses
Infix: (A + B) * C
| Step | Character | Action | Stack | Output |
| 1 | ( | Opening parenthesis, push | ( | (empty) |
| 2 | A | Operand, write to output | ( | A |
| 3 | + | Push (only ( is in stack, not an operator) | ( + | A |
| 4 | B | Operand, write to output | ( + | A B |
| 5 | ) | Pop until (, write + to output, discard ( | (empty) | A B + |
| 6 | * | Stack empty, push | * | A B + |
| 7 | C | Operand, write to output | * | A B + C |
| 8 | End | Pop all remaining operators | (empty) | A B + C * |
**Result: A B + C ***
This means: evaluate A + B first (because of parentheses), then multiply by C. Correct.
Example 3: Full Expression With Multiple Operators
Infix: A + B * C – D
| Step | Character | Action | Stack | Output |
| 1 | A | Operand | (empty) | A |
| 2 | + | Push | + | A |
| 3 | B | Operand | + | A B |
| 4 | * | Higher than +, push | + * | A B |
| 5 | C | Operand | + * | A B C |
| 6 | – | Lower than *, pop *. Equal to +, pop +. Push – | – | A B C * + |
| 7 | D | Operand | – | A B C * + D |
| 8 | End | Pop – | (empty) | A B C * + D – |
Result: A B C * + D –
How to Evaluate a Postfix Expression
Once you have the postfix expression, evaluating it is very simple using a stack.
Rules for evaluation:
- Operand: Push it onto the stack
- Operator: Pop the top two operands, apply the operator, push the result back
Example: Evaluate 2 3 4 + *
| Step | Character | Action | Stack |
| 1 | 2 | Push | 2 |
| 2 | 3 | Push | 2, 3 |
| 3 | 4 | Push | 2, 3, 4 |
| 4 | + | Pop 4 and 3, compute 3+4=7, push 7 | 2, 7 |
| 5 | * | Pop 7 and 2, compute 2*7=14, push 14 | 14 |
Answer: 14
This is exactly the same as evaluating (3 + 4) * 2 in infix.
Quick Reference: Infix vs Postfix
| Feature | Infix | Postfix |
| Operator position | Between operands | After operands |
| Parentheses needed | Yes | No |
| Human readability | Easy | Harder at first |
| Computer evaluation | Complex | Simple |
| Used in | Math notebooks | Compilers, calculators |
Where Is Infix to Postfix Used in Real Life
- Compilers: Every programming language compiler converts your code expressions into postfix internally before executing them
- Scientific calculators: Calculators convert what you type into postfix to evaluate it correctly
- Expression evaluators: Tools that evaluate math expressions from strings use postfix
- Spreadsheet software: Programs like Excel evaluate cell formulas using postfix internally
- Operating systems: Expression parsing in shell scripts and command-line tools uses this conversion
Tips for Beginners
- Always check precedence before pushing an operator. This is the most common mistake. If the stack has an operator with higher or equal precedence, pop it first.
- Parentheses never go to the output. They only help control the order inside the stack. Always discard them.
- Operands always go straight to output. They never touch the stack at all.
- At the end, empty the stack. After scanning the full expression, pop everything left in the stack and write it to output.
- Practice with simple examples first. Start with A + B, then A + B * C, then add parentheses. Build up complexity gradually.
- Draw the stack on paper. Visualizing the stack as a column and writing each step out helps more than anything else.
💡 Did You Know?
- Postfix notation was invented by Australian computer scientist Charles Hamblin in the 1950s and is also called Reverse Polish Notation.
- HP calculators from the 1970s and 1980s famously used postfix notation, and many engineers still prefer them today.
- Compilers silently convert expressions like x = a + b * c into postfix form before executing the code.
Conclusion
Infix to postfix expression conversion looks complicated the first time you see it. But once you understand that operands always go to output and operators wait in a stack based on their precedence, the whole thing clicks. The stack is doing all the heavy lifting for you.
Start by practicing the three examples in this guide on paper. Then try your own expressions like A * B + C / D or (A + B) * (C – D). Check your answers step by step against the algorithm. Within a few practice rounds, you will be converting infix to postfix expressions in your head without even thinking about it.
This concept forms the base for expression trees, compiler design, and calculator logic. Getting comfortable with it now will make every advanced topic much easier.
FAQs
1. What is the difference between infix and postfix expression?
In an infix expression, the operator is placed between two operands, like A + B. In a postfix expression, the operator comes after the operands, like A B +. Postfix does not need parentheses and is easier for computers to evaluate.
2. Why do we convert infix to postfix?
Computers cannot easily evaluate infix expressions because they need to figure out precedence and parentheses. Postfix expressions have no parentheses and follow a simple left-to-right evaluation using a stack, which makes them much faster and simpler for machines.
3. What data structure is used to convert infix to postfix?
A stack is used. Operands go directly to the output. Operators are pushed onto the stack and popped based on their precedence. Parentheses are handled separately and never written to the output.
4. What is operator precedence in infix to postfix conversion?
Operator precedence defines which operator is evaluated first. Exponent (^) has the highest precedence, followed by multiplication and division (* /), and then addition and subtraction (+ -). Higher-precedence operators are placed earlier in the postfix output.
5. Is infix to postfix conversion asked in coding interviews?
Yes. It is one of the most commonly asked stack-based problems in technical interviews and university exams. It tests your understanding of stacks, operator precedence, and expression parsing all at once.



Did you enjoy this article?