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.

OperatorDescriptionAssociativity
()Parentheses or function callLeft to Right
[]Brackets (array subscript)Left to Right
.Member selection via object nameLeft to Right
->Member selection via pointerLeft to Right
++-Postfix increment/decrementLeft to Right
++ Prefix increment/decrementRight to Left
+-Unary plus/minusRight to Left
! ~Logical negation/bitwise complementRight to Left
(type)type castRight to Left
*Dereference Or indirection operatorRight to Left
&Address of operatorRight to Left
sizeofDetermine size in bytes Right to Left
*, /, %Multiplication/division/modulusLeft to Right
+  –Addition/subtractionLeft to Right
<<  >>Bitwise shift left, Bitwise shift rightLeft to Right
<  <=Relational less than/less than or equal toLeft to Right
>  >=greater than/greater  than or equal toLeft to Right
== , !=equal to/is not equal toLeft to Right
&Bitwise ANDLeft to Right
^Bitwise exclusive ORLeft to Right
|Bitwise inclusive ORLeft to Right
&&Logical ANDLeft to Right
||Logical ORLeft to Right
?:Ternary operatorRight to Left
=AssignmentRight to Left
+=  -=Addition/subtraction Right to Left
*=  /=Multiplication/division assignmentRight to Left
%=  &=Modulus/bitwise AND assignmentRight to Left
^=  |=Bitwise exclusive/inclusive OR assignmentRight to Left
<<=  >>=Bitwise shift left/right assignmentRight to Left
,Comma expressionsLeft to Right

Leave a Reply

Your email address will not be published. Required fields are marked *