Friday, July 29, 2016

C Basics - Operator Precedence and Associativity in C

What happens when an expression contains two operators of equal priority? the tie between them is settled using the associativity of the operators. Operator precedence determines which operator will be performed first in a group of operators with different precedence’s. For instance 5 + 3 * 2 is calculated as 5 + (3 * 2), giving 11, and not as (5 + 3) * 2, giving 16.

The order of operations (or operator precedence) is a collection of rules that define which procedures to perform first in order to evaluate a given mathematical expression.

The associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses.

The operator associativity rules define the order in which adjacent operators with the same precedence level are evaluated. For instance the expression 8 - 3 - 2 is calculated as (8 - 3) - 2, giving 3, and not as 8 - (3 - 2), giving 7. In this case we say that subtraction is left associative meaning that the left most subtraction must be done first.

Associativity can be of two types—Left to Right or Right to Left. Left to Right associativity means that the left operand must be unambiguous. Unambiguous in what sense? It must not be involved in evaluation of any other sub-expression. Similarly, in case of Right to Left associativity the right operand must be unambiguous.

Consider the expression
a = 3 / 2 * 5 ;
Here there is a tie between operators of same priority, that is between / and *. Both enjoy Left to Right associativity.
Operator Left Right
/ 3 2 *5
* 3/2 5
Since both / and * have L to R associativity and only / has unambiguous left operand it is performed earlier.

Consider the expression
a = b = 3 ;
Here both assignment operators have the same priority and same associativity (Right to Left).
Operator Left Right
= a b = 3
= a = b 3
Since both = have R to L associativity and only the second = has unambiguous right operand the second = is performed earlier.

Consider the expression
z = a * b + c / d ; 
Here * and / enjoys same priority and same associativity (Left to Right).
Operator Left Right
* a b
/ c d
Here since left operands for both operators are unambiguous Compiler is free to perform * or / operation as per its convenience.

Precedence Description Operator Associativity
2
highest
Postfix increment ++ Left to Right
Postfix decrement -- Left to Right
Function call () Left to Right
Array subscripting [] Left to Right
Element selection by reference -> Left to Right
Element selection through pointer . Left to Right
3 Prefix increment ++ Right to Left
Prefix decrement -- Right to Left
Unary plus + Right to Left
Unary minus - Right to Left
Logical NOT ! Right to Left
Bitwise NOT ~ Right to Left
Type cast (type) Right to Left
Indirection * Right to Left
Address of & Right to Left
Size-of sizeof Right to Left
5 Multiplication * Left to Right
Division / Left to Right
Modulus % Left to Right
6 Addition + Left to Right
Subtraction - Left to Right
7 Bitwise Left Shift << Left to Right
Bitwise Right Shift >> Left to Right
8 Less than < Left to Right
Less than or equal to <= Left to Right
Greater than > Left to Right
Greater than or equal to >= Left to Right
9 Equal to == Left to Right
Not equal to != Left to Right
10 Bitwise AND & Left to Right
11 Bitwise exclusive OR ^ Left to Right
12 Bitwise OR | Left to Right
13 Logical AND && Left to Right
14 Logical OR || Left to Right
15 Conditional ? : Right to Left
16 Direct assignment = Right to Left
Addition assignment += Right to Left
Subtraction assignment -= Right to Left
Multiplication assignment *= Right to Left
Division assignment /= Right to Left
Modulo assignment %= Right to Left
Bitwise left shift assignment <<= Right to Left
Bitwise right shift assignment >>= Right to Left
Bitwise AND assignment &= Right to Left
Bitwise XOR assignment ^= Right to Left
Bitwise OR assignment |= Right to Left
18
lowest
Comma , Right to Left

Hierarchy of Operators in C:
All operators in C are ranked according to their precedence. A statement may have two or more arithmetic operators. The priority or precedence in which the operations in an arithmetic statement are performed is called the hierarchy of operations.

Priority Operators Description
1st */ % Multiplication, division, modular division
2nd + - Addition, subtraction
3rd = Assignment
Within parentheses the same hierarchy as mentioned in table above is operative. Also, if there are more than one set of parentheses, the operations within the innermost parentheses would be performed first, followed by the operations within the second innermost pair and so on.

Stepwise evaluation of this expression is shown below:

i = 2 * 3 / 4 + 4 / 4 + 8 - 2 + 5 / 8
i = 6 / 4 + 4 / 4 + 8 - 2 + 5 / 8  operation: * 
i = 1 + 4 / 4 + 8 - 2 + 5 / 8  operation: / 
i = 1 + 1+ 8 - 2 + 5 / 8  operation: / 
i = 1 + 1 + 8 - 2 + 0   operation: / 
i = 2 + 8 - 2 + 0    operation: + 
i = 10 - 2 + 0    operation: + 
i = 8 + 0    operation : - 
i = 8     operation: +



Related topics:
Overview of Operators in C   |   Arithmetic Operators in C   |   Relational Operators in C   |   Logical Operators in C   |   Bitwise Operators in C   |   Compound Assignment Operators in C   |   Conditional Operators in C   |   Miscellaneous Operators in C

List of topics: C Programming

No comments:

Post a Comment