When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators. 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. It means it can not be involved in evaluation of any other sub-expression. Similarly, in case of Right to Left associativity the right operand must be unambiguous.
Let us understand this with an example.
EX 1 : Consider the expression a = 3 / 2 * 5 ;
Here there is a tie between operators of same priority, that is between / and *. This tie is settled using the associativity of / and *. But both enjoy Left to Right associativity. The following diagram shows for each operator which operand is unambiguous and which is not.
Since both / and * have L to R associativity and only / has unambiguous left operand (necessary condition for L to R associativity) it is performed earlier.
EX 2 : Consider one more expression a = b = 3 ;
Here both assignment operators have the same priority and same associativity (Right to Left). The following Figure shows for each operator which operand is unambiguous and which is not.
Since both = have R to L associativity and only the second = has unambiguous right operand (necessary condition for R to L associativity) the second = is performed earlier.
EX 3 : Consider yet another expression z = a * b + c / d ;
I request you to share this page on your favorite social book marking site with the below link.
Here * and / enjoys same priority and same associativity (Left to Right). The below Figure shows for each operator which operand is unambiguous and which is not.
Here since left operands for both operators are unambiguous Compiler is free to perform * or / operation as per its convenience since no matter which is performed earlier the result would be same.
54.1
The previous post of the blog deals with Priority of mathematical operators in C Programming.
Related Posts :
IF STATEMENT
MULTIPLE STATEMENTS IN IF
IF AND ELSE
NESTED IF AND ELSE
BREAK
CONTINUE AND DO WHILE IN C LANGUAGE
SWITCH IN C PROGRAMMING
FUNCTIONS IN C PROGRAMMING
Functions and usage in C part two
Coding in C functions
Let us understand this with an example.
EX 1 : Consider the expression a = 3 / 2 * 5 ;
Here there is a tie between operators of same priority, that is between / and *. This tie is settled using the associativity of / and *. But both enjoy Left to Right associativity. The following diagram shows for each operator which operand is unambiguous and which is not.
Since both / and * have L to R associativity and only / has unambiguous left operand (necessary condition for L to R associativity) it is performed earlier.
EX 2 : Consider one more expression a = b = 3 ;
Here both assignment operators have the same priority and same associativity (Right to Left). The following Figure shows for each operator which operand is unambiguous and which is not.
Since both = have R to L associativity and only the second = has unambiguous right operand (necessary condition for R to L associativity) the second = is performed earlier.
EX 3 : Consider yet another expression z = a * b + c / d ;
I request you to share this page on your favorite social book marking site with the below link.
Here * and / enjoys same priority and same associativity (Left to Right). The below Figure shows for each operator which operand is unambiguous and which is not.
Here since left operands for both operators are unambiguous Compiler is free to perform * or / operation as per its convenience since no matter which is performed earlier the result would be same.
54.1
The previous post of the blog deals with Priority of mathematical operators in C Programming.
Related Posts :
IF STATEMENT
MULTIPLE STATEMENTS IN IF
IF AND ELSE
NESTED IF AND ELSE
BREAK
CONTINUE AND DO WHILE IN C LANGUAGE
SWITCH IN C PROGRAMMING
FUNCTIONS IN C PROGRAMMING
Functions and usage in C part two
Coding in C functions
cheater you copied letusc book
ReplyDelete