Infix, Postfix and Prefix Notation
Infix Expression or Notation
In this type of notation operator is in between the two operands. E.g. A+B-C (where A,B,C are operands)
Postfix Expression or Notation
In this type of notation operator is placed after the operands. E.g. AB+C-
It is also known as Reversed Polish notation.
Prefix Expression or Notation
In this notation or expression operator is placed before the operands. E.g. – + A B C
Overall all these three expressions produce same result. Now then why we need these separate notations.
Because Infix expressions can be easily read by humans but pre/post expressions are easier to parse for a machine. Pre/Postfix notation are easily evaluated by machine. As algorithm for infix notation is complex and is inefficient in terms of time and complexity.
Consider the below e.g.
3+8/2
When we start resolving this notation then first thing comes in mind, operator precedence. Either to solve 3+8 or 8/2. So, we follow some operator precedence rules like BODMASS (i.e. Brackets first then Other then Division then Multiplication then Addition and finally Subtraction) rule to make the job easy.
Result = 3+4 = 7
Operator of higher precedence is considered firstly over lower precedence. However, if there is parentheses present within the expression there then order get affected and that expression will evaluate first. Also, if no parentheses exist within expression and any of operators have same precedence then associativity or left to right/right to left ordering is used. Associativity of postfix ++ is left to right and associativity of prefix ++ is right to left. Associativity determines the direction in which they execute.
Now when we write the above expression into Prefix : +3/82
Postfix :382/+. Computers generally uses stack to evaluate the expressions.
Operator | Description | Associativity |
() | Parentheses or function call | Left to Right |
[] | Brackets (array subscript) | Left to Right |
. | Member selection via object name | Left to Right |
-> | Member selection via pointer | Left to Right |
++- | Postfix increment/decrement | Left to Right |
++ | Prefix increment/decrement | Right to Left |
+- | Unary plus/minus | Right to Left |
! ~ | Logical negation/bitwise complement | Right to Left |
(type) | type cast | Right to Left |
* | Dereference Or indirection operator | Right to Left |
& | Address of operator | Right to Left |
sizeof | Determine size in bytes | Right to Left |
*, /, % | Multiplication/division/modulus | Left to Right |
+ – | Addition/subtraction | Left to Right |
<< >> | Bitwise shift left, Bitwise shift right | Left to Right |
< <= | Relational less than/less than or equal to | Left to Right |
> >= | greater than/greater than or equal to | Left to Right |
== , != | equal to/is not equal to | Left to Right |
& | Bitwise AND | Left to Right |
^ | Bitwise exclusive OR | Left to Right |
| | Bitwise inclusive OR | Left to Right |
&& | Logical AND | Left to Right |
|| | Logical OR | Left to Right |
?: | Ternary operator | Right to Left |
= | Assignment | Right to Left |
+= -= | Addition/subtraction | Right to Left |
*= /= | Multiplication/division assignment | Right to Left |
%= &= | Modulus/bitwise AND assignment | Right to Left |
^= |= | Bitwise exclusive/inclusive OR assignment | Right to Left |
<<= >>= | Bitwise shift left/right assignment | Right to Left |
, | Comma expressions | Left to Right |