Infix to Postfix Converter
Type any infix expression and get the postfix form instantly — along with the complete stack trace showing exactly which operator was pushed, popped and why.
Step-by-step stack trace
| # | Token | Action | Stack | Postfix output |
|---|
Want to watch the stack fill and drain one token at a time? Open the animated stack visualiser →
Which converter do you need?
The tool above is a free infix to postfix converter online — nothing to install and no sign-up. For the other directions, the working shown a particular way, or the infix to postfix converter code in a language you can run, start here.
The reverse direction — turn an RPN expression back into readable arithmetic, with the brackets it needs.
Infix to prefix converterPolish notation, step by step. Prefix to infix and prefix to postfix are on the same page.
Postfix expression calculatorEvaluate postfix, infix or prefix to a number and watch the value stack change.
Infix to postfix converter using a stackWatch an infix to postfix converter using stack operations run one token at a time, with every push and pop animated.
Practice questions with answersThirty-five problems graded from basic precedence to exam level, every answer machine-checked.
Infix to postfix converter rulesThe seven conversion rules, the operator priority table and the pseudocode behind them.
Infix to postfix converter in CComplete, compile-tested converter code in C, with an array stack and a linked-list variant.
Infix to postfix converter in PythonThe same infix to postfix converter Python code, with a step-by-step trace and a postfix evaluator.
Infix to postfix converter in JavaThe converter in Java with ArrayDeque as the stack, plus a postfix evaluator.
Infix to postfix converter in C++The converter in C++ using std::stack, with a postfix evaluator.
An infix to postfix converter turns an ordinary arithmetic expression into
Reverse Polish Notation (RPN) by reading the expression once from
left to right and holding each operator on a stack until its position is fixed.
Enter A + B * C and the converter returns A B C * +.
An infix to postfix converter gives three benefits: it removes every bracket from the expression, it removes operator precedence from the evaluation step, and it produces a form a machine evaluates in a single pass with one stack. Compilers, command editors and calculators all run the conversion before evaluating anything.
An infix to postfix converter has four parts: a tokenizer that splits the input string into operands and operators, a precedence table, an operator stack, and an output queue.
How to use this infix to postfix converter
Use this infix to postfix converter step by step: type an expression, read the postfix result, then check the trace underneath. Nothing is uploaded — the whole conversion runs in your browser as you type.
-
Type the infix expression.
Operands can be single letters (
A), whole words (rate) or numbers (3.5). Operators are+,-,*,/,%and^. Round, square and curly brackets all work. - Read the postfix result. It updates as you type — no need to press anything. Use the copy button to take it into your assignment or code.
- Check the trace. The table underneath shows the stack and the output after every single token, with a sentence explaining the decision. That is the part your lecturer wants to see, not just the final answer.
What is infix notation?
Infix notation is the ordinary way of writing arithmetic: the
operator sits between its two operands, as in A + B. It reads
naturally to humans, but it is ambiguous on its own — A + B * C only
means "multiply first" because we all agree on precedence rules, and brackets exist
purely to override those rules.
What is postfix notation?
Postfix notation — also called Reverse Polish Notation
(RPN) — puts the operator after its operands: A B +.
Once an expression is in postfix there are no brackets and no precedence rules left
to apply. The order of the symbols alone fixes the order of evaluation, which is why
compilers, calculators and stack-based virtual machines convert to postfix before
evaluating anything.
| Infix | Postfix (RPN) | Prefix (Polish) |
|---|---|---|
| A + B | A B + | + A B |
| A + B * C | A B C * + | + A * B C |
| (A + B) * C | A B + C * | * + A B C |
| A * B + C / D | A B * C D / + | + * A B / C D |
How the conversion works
The converter runs Dijkstra's shunting-yard algorithm. It reads the infix expression left to right, sends operands straight to the output, and parks operators on a stack until it is certain where they belong. An operator waits on the stack while everything above it binds more tightly; the moment a weaker operator or a closing bracket arrives, it is popped to the output.
That single rule is what turns A + B * C into A B C * +:
when * arrives, it outranks the + already on the stack, so
+ stays put and waits its turn.
The full rule set, pseudocode and a hand-worked dry run live on the infix to postfix conversion algorithm page. If you specifically need to show the stack in your answer, use the stack walkthrough.
Operator precedence and associativity
Precedence decides which operator wins when two compete; associativity breaks the tie when they are equal. These are the values this converter uses, and they match the convention used in almost every data structures course.
| Operator | Meaning | Precedence | Associativity |
|---|---|---|---|
| ^ | Exponentiation | 3 (highest) | Right to left |
| * / % | Multiply, divide, modulo | 2 | Left to right |
| + - | Add, subtract | 1 (lowest) | Left to right |
| ( ) | Grouping | — | Pushed as a marker, never output |
Right associativity is the reason a ^ b ^ c becomes
a b c ^ ^ and not a b ^ c ^ — it is read as
a ^ (b ^ c). Every other operator here associates left to right.
Several converter sites group % with ^ at the same
precedence level. That grouping is wrong. Modulus shares its level with
* and / in C, Java, Python and every standard
implementation, so a % b ^ c converts to a b c ^ %.
The incorrect grouping produces a b % c ^, which is a different
expression. Paste either into the converter above to check.
Infix to postfix examples with answers
Two worked conversions, both numeric so the value gives a free check on the answer. Every row below comes from the converter above.
Example #1: 6*4+2^5-3
6 * 4 + 2 ^ 5 - 3 converts to
6 4 * 2 5 ^ + 3 - and evaluates to
53. The conversion takes 10 steps.
| # | Token | Action | Stack | Output |
|---|---|---|---|---|
| 1 | 6 | Operand — send it straight to the output. | empty | 6 |
| 2 | * | The stack is empty — push "*". | * | 6 |
| 3 | 4 | Operand — send it straight to the output. | * | 6 4 |
| 4 | + | Pop * to the output (precedence at least "+"), then push "+". | + | 6 4 * |
| 5 | 2 | Operand — send it straight to the output. | + | 6 4 * 2 |
| 6 | ^ | "^" binds tighter than "+" on top — push "^". | + ^ | 6 4 * 2 |
| 7 | 5 | Operand — send it straight to the output. | + ^ | 6 4 * 2 5 |
| 8 | - | Pop ^, + to the output (precedence at least "-"), then push "-". | - | 6 4 * 2 5 ^ + |
| 9 | 3 | Operand — send it straight to the output. | - | 6 4 * 2 5 ^ + 3 |
| 10 | end | End of input — pop the remaining operator - to the output. | empty | 6 4 * 2 5 ^ + 3 - |
Example #2: 4*((2+6)/3)-2^2^3
4 * ((2 + 6) / 3) - 2 ^ 2 ^ 3 converts to
4 2 6 + 3 / * 2 2 3 ^ ^ - and evaluates to
-245.333. The conversion takes 18 steps.
The two ^ operators are the point of this example. Exponentiation
associates right to left, so the second ^ does not pop the first
one, and the output ends 2 2 3 ^ ^ rather than
2 2 ^ 3 ^.
| # | Token | Action | Stack | Output |
|---|---|---|---|---|
| 1 | 4 | Operand — send it straight to the output. | empty | 4 |
| 2 | * | The stack is empty — push "*". | * | 4 |
| 3 | ( | Push "(" onto the stack as a marker. | * ( | 4 |
| 4 | ( | Push "(" onto the stack as a marker. | * ( ( | 4 |
| 5 | 2 | Operand — send it straight to the output. | * ( ( | 4 2 |
| 6 | + | The top of the stack is "(" — push "+". | * ( ( + | 4 2 |
| 7 | 6 | Operand — send it straight to the output. | * ( ( + | 4 2 6 |
| 8 | ) | Pop + to the output, then discard the matching "(". | * ( | 4 2 6 + |
| 9 | / | The top of the stack is "(" — push "/". | * ( / | 4 2 6 + |
| 10 | 3 | Operand — send it straight to the output. | * ( / | 4 2 6 + 3 |
| 11 | ) | Pop / to the output, then discard the matching "(". | * | 4 2 6 + 3 / |
| 12 | - | Pop * to the output (precedence at least "-"), then push "-". | - | 4 2 6 + 3 / * |
| 13 | 2 | Operand — send it straight to the output. | - | 4 2 6 + 3 / * 2 |
| 14 | ^ | "^" binds tighter than "-" on top — push "^". | - ^ | 4 2 6 + 3 / * 2 |
| 15 | 2 | Operand — send it straight to the output. | - ^ | 4 2 6 + 3 / * 2 2 |
| 16 | ^ | "^" is right-associative, so an equal-precedence "^" stays put — push "^". | - ^ ^ | 4 2 6 + 3 / * 2 2 |
| 17 | 3 | Operand — send it straight to the output. | - ^ ^ | 4 2 6 + 3 / * 2 2 3 |
| 18 | end | End of input — pop the remaining operators ^, ^, - to the output. | empty | 4 2 6 + 3 / * 2 2 3 ^ ^ - |
Quick reference table
Common textbook expressions and their answers. Click any infix to postfix converter example with solution below to load it into the tool above and see the full stack trace, not just the final line.
| Infix expression | Postfix answer | Why |
|---|---|---|
| A + B | A B + | The simplest case |
| A + B * C | A B C * + | * binds tighter than + |
| (A + B) * C | A B + C * | Brackets force the addition first |
| A * B + C * D | A B * C D * + | Both products resolve before the sum |
| A - B - C | A B - C - | Left associative, so it is (A - B) - C |
| A ^ B ^ C | A B C ^ ^ | Right associative, so it is A ^ (B ^ C) |
| A + B * C - D / E | A B C * + D E / - | Two products, then the subtraction |
| (A + B) * (C - D) | A B + C D - * | Each bracket flushes independently |
| A + B * C - (D / E ^ F) * G | A B C * + D E F ^ / G * - | Precedence and brackets combined |
Using it as an infix to postfix calculator
If every operand is a number, the tool goes one step further and evaluates the
postfix expression for you — the Evaluates to row appears automatically.
Try 3 + 4 * 2 / (1 - 5) ^ 2: the postfix form is
3 4 2 * 1 5 - 2 ^ / + and the value is 3.5. Mixed
expressions with variables still convert; they simply have nothing to evaluate.
Applications
Infix to postfix conversion has five applications.
- Compilers convert expressions to postfix before generating code.
- Calculators convert once, then evaluate the postfix form with a value stack.
- Stack machines run postfix directly — the Java Virtual Machine and CPython both execute stack bytecode.
- Command editors parse arithmetic arguments through the same conversion.
- Data structures courses use the conversion as the first real application of a stack.
Humans read infix easily and pick out the order of operators by eye. A machine cannot separate operators and parentheses that cheaply, which is why postfix conversion comes first.
Frequently asked questions
How do you convert infix to postfix?
Scan the infix expression from left to right. Send every operand directly to
the output. Push ( onto a stack. When you hit ), pop
operators to the output until the matching ( appears and discard
the pair. When you hit an operator, first pop any stacked operator with greater
or equal precedence to the output, then push the new one. At the end, pop
whatever remains on the stack.
Why convert an infix expression to postfix at all?
Postfix removes brackets and precedence entirely, so a machine can evaluate it with one pass and one stack — push operands, and on each operator pop two values and push the result. Compilers and calculators do this because parsing infix directly on every evaluation would be far slower and much harder to implement.
Is postfix notation the same as Reverse Polish Notation?
Yes. Postfix and Reverse Polish Notation (RPN) are two names for the same thing: the operator written after its operands. Prefix notation — the operator written first — is called Polish notation.
What happens to the brackets during conversion?
Brackets never appear in the output. A ( is pushed purely as a
marker so the algorithm knows where to stop popping; when the matching
) arrives, both are thrown away once the operators between them
have been flushed to the output. A valid postfix expression contains no
brackets at all.
Does this converter handle multi-character operands and decimals?
Yes. Variable names such as rate or x1 and numbers
such as 3.5 or 250 are treated as single operands.
Many classroom implementations only handle one character per operand, which is
why 12 + 3 breaks in them but works here.
What is the time complexity of the conversion?
O(n) time and O(n) space for an expression of n tokens. Each token is read once and each operator is pushed once and popped once, so the total work stays linear no matter how deeply nested the brackets are.
Why does a ^ b ^ c give a b c ^ ^ instead of a b ^ c ^?
Exponentiation is right associative, so a ^ b ^ c means
a ^ (b ^ c). The rule inside the algorithm is that an incoming
right-associative operator only pops operators of strictly greater
precedence, so the first ^ stays on the stack when the second one
arrives.
Can I share a converted expression?
Yes — the address bar updates as you type, so copying the URL gives you a link that reopens the page with the same expression already converted.
Can you convert infix to postfix without using a stack?
Yes. Build an expression tree from the infix expression and read the tree in post-order, or use recursive descent parsing. Both methods still use a stack indirectly through the call stack, and both take more code than the single-pass stack method.
What is the difference between infix, postfix and prefix?
The operator position differs. Infix writes the operator between its operands (A + B), postfix writes the operator after them (A B +), and prefix writes the operator before them (+ A B). Postfix and prefix need no parentheses; infix needs both parentheses and precedence rules.
Which infix to postfix question appears most often in exams?
Expressions of the form A - B - C and A / B / C. Pushing on equal precedence instead of popping gives A B C - -, which means A - (B - C). The correct answer is A B - C -.
Keep going
Watch an infix to postfix converter using stack operations run one token at a time, with every push and pop animated.
The conversion algorithmFull rules, pseudocode, precedence table, dry run and complexity analysis.
Infix to postfix program in CA complete, compilable C program with an array stack — explained line by line.