<p>In <strong>Prefix notation</strong>, the operator comes before the operands (e.g., <code>+a b</code>). <br />
In <strong>Postfix notation</strong>, the operator comes after the operands (e.g., <code>a b+</code>). <br />
In <strong>Infix notation</strong>, the operator comes in between the operands (e.g., <code>a+b</code>).</p>
<p>Most practical languages use infix notation for binary operators, but some use prefix or postfix.</p>
<p>So, what are the advantages and disadvantages of each type of notation?</p>

<syntax><operators>
What are the pros and cons of each type of operator notation (prefix/postfix/infix)?
CC BY-SA 4.0
2023-05-17T17:02:40.157Z
There's also [mixfix](https://agda.readthedocs.io/en/v2.6.3/language/mixfix-operators.html)! It subsumes prefix, postfix, and infix, for better or for worse.
James Martin
CC BY-SA 4.0
2023-05-17T17:10:53.557Z
TIL Lisp is an esolang.
Adám
CC BY-SA 4.0
2023-05-17T17:17:08.257Z
Most practical languages use prefix too (`+`, `-`, `~`, `!`). Many use postfix (`?`, `!!`)
user
CC BY-SA 4.0
2023-05-17T17:17:43.013Z
@user I meant for binary operators only
The Thonnu
CC BY-SA 4.0
2023-05-17T17:18:58.070Z
@Adám And on the postfix side, there's Forth and Factor
user
<p>Infix notation is primarily borrowed from math. If you consider function calls as a sort of operator, you'll see that it is (most often) a prefix one. As for their pros and cons, the following come to mind:</p>
<ul>
<li>Infix is, as said above, familiar to mathematicians, which has influenced general language design significantly in various areas.</li>
<li>Prefix and postfix notations can be parsed unambiguously without operator precedence. (The first one by a recursive parser, the second -- by a stack-based one).</li>
<li>Prefix notation (arguably) allows the programmer to grasp the code more easily while reading it left-to-right. Essentially, it allows to focus on the data knowing in advance what operators it is fed to. It bears similarity to the natural languages' grammatic forms, in fact: &quot;Add two and the difference between five and four&quot; ~ <code>+ 2 - 5 4</code>. On the other hand, since this approach technically requires no delimiters/parentheses, it is easy to get lost in context, whereas with infix notation you'll usually be able to start from anywhere in the middle by looking for the nearest parentheses. Regardless, postfix is probably the worst approach in this sense: you need to memorize the stack before knowing the meaning of any specific item. Easy to parse for a machine, but hard to grasp for a human.</li>
<li>Infix notation doesn't scale well for higher -arities. The only way I know of is adding new operators to work in a group (for example, that's what Python's inline <code>a if b else c</code> does). That's why even languages that stick to infix for arithmetics tend to prefer either prefix or postfix for general functions</li>
</ul>

CC BY-SA 4.0
2023-05-17T17:24:07.113Z
2023-05-17T18:34:27.143Z
2023-05-17T18:34:27.143Z
abel1502
CC BY-SA 4.0
2023-05-17T18:52:28.530Z
An example of a language that decided to use infix for everything is Smalltalk, and this concept was continued by Objective-C.
Bbrk24
<p>They really are just how people think about applying operators.</p>
<p><em>Infix</em> is common arithmetic notation. Everyone learns <code>a + b</code> in school.</p>
<p><em>Prefix</em> is really a function call. <code>+ a b</code> is just like <code>foo a b</code> with no special parsing.</p>
<p><em>Postfix</em> is a stack operation. <code>a b +</code> means push, push, and then pop/apply.</p>
<p>Ultimately users are applying an operator (a function), so the notation reflects how users think about it.</p>

CC BY-SA 4.0
2023-05-17T17:18:20.043Z
2023-05-17T17:18:20.043Z
chrisaycock
# What are the pros and cons of each type of operator notation (prefix/postfix/infix)?
Posted by: username_0 on 2023-05-17
In **Prefix notation**, the operator comes before the operands (e.g., `+a b`).   
In **Postfix notation**, the operator comes after the operands (e.g., `a b+`).   
In **Infix notation**, the operator comes in between the operands (e.g., `a+b`).

Most practical languages use infix notation for binary operators, but some use prefix or postfix.

So, what are the advantages and disadvantages of each type of notation?
**Tags:** syntax operators
> **Comments:**
> **username_1**: There's also [mixfix](https://agda.readthedocs.io/en/v2.6.3/language/mixfix-operators.html)! It subsumes prefix, postfix, and infix, for better or for worse.  
> **username_2**: TIL Lisp is an esolang.  
> **username_3**: Most practical languages use prefix too (`+`, `-`, `~`, `!`). Many use postfix (`?`, `!!`)  
> **username_0**: @user I meant for binary operators only  
> **username_3**: @Adám And on the postfix side, there's Forth and Factor  
---
## 2 answers
### Answer by username_6 on 2023-05-17. Score: 7 | Accepted answer
They really are just how people think about applying operators.

*Infix* is common arithmetic notation. Everyone learns `a + b` in school.

*Prefix* is really a function call. `+ a b` is just like `foo a b` with no special parsing.

*Postfix* is a stack operation. `a b +` means push, push, and then pop/apply.

Ultimately users are applying an operator (a function), so the notation reflects how users think about it.
---
### Answer by username_4 on 2023-05-17. Score: 5
Infix notation is primarily borrowed from math. If you consider function calls as a sort of operator, you'll see that it is (most often) a prefix one. As for their pros and cons, the following come to mind:

* Infix is, as said above, familiar to mathematicians, which has influenced general language design significantly in various areas.
* Prefix and postfix notations can be parsed unambiguously without operator precedence. (The first one by a recursive parser, the second -- by a stack-based one).
* Prefix notation (arguably) allows the programmer to grasp the code more easily while reading it left-to-right. Essentially, it allows to focus on the data knowing in advance what operators it is fed to. It bears similarity to the natural languages' grammatic forms, in fact: "Add two and the difference between five and four" ~ `+ 2 - 5 4`. On the other hand, since this approach technically requires no delimiters/parentheses, it is easy to get lost in context, whereas with infix notation you'll usually be able to start from anywhere in the middle by looking for the nearest parentheses. Regardless, postfix is probably the worst approach in this sense: you need to memorize the stack before knowing the meaning of any specific item. Easy to parse for a machine, but hard to grasp for a human.
* Infix notation doesn't scale well for higher -arities. The only way I know of is adding new operators to work in a group (for example, that's what Python's inline `a if b else c` does). That's why even languages that stick to infix for arithmetics tend to prefer either prefix or postfix for general functions
> **Comments:**
> **username_5** (2023-05-17): An example of a language that decided to use infix for everything is Smalltalk, and this concept was continued by Objective-C.  
---
