Operator precedence describes the way in which the operations are evaluated. When we have several operations in an expression, each part is evaluated and resolved in a predetermined order decided by the operator precedence.
Operators having higher precedence are solved first as compared to the lower precedence operators.
5+3*7 // it gives 26
This is so because the multiplication operator (“*”) has higher precedence over the addition operator (“+”), thus the expression 3 * 7 will be solved first and the result of it (i.e., 21) becomes the right operand for the addition, and then the addition is performed as 5 + 21 which returns 26.
Associativity defines how the operators having the same precedence are evaluated.
10*5/2 // it gives 25
(“*”) and (“/”) have the same precedence and “left to right” associativity; therefore, 10*5 evaluated first, then its result (50) becomes the second operand for (“/”) and after that 50/2 is evaluated which gives 25.
Operator Meaning of operator Associativity Rank ()->. Functional call (Parentheses)Array element referenceIndirect member selectionDirect member selection Left to right 1 !~+-++--&*sizeof(type) Logical negationBitwise(1 's) complementUnary plusUnary minusIncrementDecrementAddress-Of OperatorPointer reference(indirection)Returns the size of an objectType cast(conversion) Right to left 2 */% MultiplyDivideRemainder(modulo-division) Left to right 3 +- AdditionSubtraction Left to right 4 <<>> Left shiftRight shift Left to right 5 <<=>>= Less thanLess than or equalGreater thanGreater than or equal Left to right 6 ==!= Equal toNot equal to Left to right 7 & Bitwise AND Left to right 8 ^ Bitwise exclusive OR Left to right 9 | Bitwise OR Left to right 10 && Logical AND Left to right 11 || Logical OR Left to right 12 ?: Conditional Operator Left to right 13 =*=/=%=+=-=&=^=|=<<=>>= Assignment OperatorShort-Hand AssignmentOperators Right to left 14 , Comma(separator) Left to right 15