0% found this document useful (0 votes)
14 views5 pages

Operator Precedence & Associativity

The document discusses the precedence and associativity of operators in the C programming language, explaining how expressions with multiple operators are evaluated based on defined rules. It provides examples to illustrate how different operators are prioritized and how parentheses can alter the order of evaluation. Additionally, it covers the role of associativity in resolving ambiguities when operators of the same precedence appear in an expression.

Uploaded by

mghwdcxs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Operator Precedence & Associativity

The document discusses the precedence and associativity of operators in the C programming language, explaining how expressions with multiple operators are evaluated based on defined rules. It provides examples to illustrate how different operators are prioritized and how parentheses can alter the order of evaluation. Additionally, it covers the role of associativity in resolving ambiguities when operators of the same precedence appear in an expression.

Uploaded by

mghwdcxs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
oe 4.11 Precedence and Associativity of Operators For evaluation of expressions having more than one operator, there are certain precedence and associativity rules defined in C. Let us see what these rules are and why are they required. Consider the following expression- 24385 Here we have two operators- addition and multiplication operators. If addition is performed before ‘multiplication then result will be 25 and if multiplication is performed before addition then the result will be 17. In C language, operators are grouped together and each group is given a precedence level. The precedence of all the operators is given in the following table. The upper rows in the table have higher precedence and it decreases as we move down the table. Thus the operators with precedence level 1 have highest precedence and with precedence level 15 have lowest precedence. So whenever an expression contains more than one operator, the operator with a higher precedence is evaluated first. For example in the above expression, multiplication Will be performed before addition since multiplication operator has higher precedence than the addition operator. Precedence and Assox Description Precedence ‘Assoclativigy level Fonction eall —— 1 Left to Right ‘Arrow operator Dot operator Unary plus Unary minus Increment Decrement Logical NOT 2 Right 10 Left : One's complement * Indiretion & Address (atatype) Type cast sizeof Size in bytes . Moltipication ! Division 3 Leftto Right % Modulus . + ‘Addition ¢ Taftto Right - Subtraction < Left shift 3 Tettto Right > Right shift < Less than < Less than or equal to > Greater than 6 Left to Right be Greater than or equal to Nixeql 1 Let toRign & Bitwise AND # Left to Right a Bitwise XOR 9 Left to Right i Bitwise OR 10 Left to Right a Logical AND. 1 Left to Right Wr ‘Logical OR 12 Left to Right ze ‘Conditional operator 13 Right 10 Left Assignment operators 4 Right 10 Left >= ‘Comma operator 5 Teftto Right Scanned with CamScanner a | “nt Let us take some expressions and see how they will be evaluated- @ x= abec Here + operator has higher precedence than < and =, and < has more precedence than =, so first at yj evaluated, then < operator will be evaluated, and at last the whole value will be assigned to x. If initia, are a=2,b=6, c=9, then final value of x will be 1. Gil) x*=a4b | Here + operator has higher precedence than "=, so a+b will be evaluated before compound assignment, Thi interpreted as x=x* (a+b) and not as x = xctasb. If initial values are x<5,a=2,b=6, then final valu oj be 13. Gi) x = acd] |b=c Here order of evaluation of operators will be x will be 1 In the above examples we have considered expressions that contain operators having different precedeny levels. Now consider a situation when two operators with the same precedence occur in an expression, Fy example- 5416/2+4 Here / and + have higher precedence than + operator, so they will be evaluated before addition. But / and+ have same precedence, so which one of them will be evaluated first still remains a problem. If / is evaluated before +, then the result is 37 otherwise the result is 7. Similarly consider this expression- 20-7-5-2-1 Here we have four subtraction operators, which of course have the same precedence level. If we decide w evaluate from left to right then answer will be 5, and if we evaluate from right to left the answer will be 17.10 solve these types of problems, an associativity property is assigned to each operator. Associaivity of te operators within same group is same. All the operators either associate from left to right or from right tl The associativity of all. operators is also given in the precedence table. Now again consider the above tro expressions- 5+16/2*4 Since / and + operators associate from left to right, / will be evaluated before * and the correct result is 37. 20-7-5-2-1 The subtraction operator associates from left to right so the value of this expression is 5. The assignment operator associates from ri this- If initial values are a=2, c=4, then final vale ight to left. Suppose we have a multiple assignment expression lit 5 Here initially the integer value 5 is assigned to variabl variable y. The value of expression z: i le 2 and then value of expression 2-5 is assigned ® 's 5,80 5 is assigned to variable y and now the value of expression ye ‘pression is assigned to x and the value of whole expression x-y=#"5 becomes 5. ‘The conditional operator also associates form right to left. Consider this expression« x?yra?bic Since the conditional operator is right associative, this expression is . sion is ¢ 7 x?y: (a2b:c) onsidered as If x is non zero then the value of this expression will be e qual to y. Iti ss expression ti bbe equal tothe value of expression (a7b:c) i.e. equal to if a is nom oe oe ae OF this exer Dif. is non zero and equal toc if a is 2e10. 4.12 Role of Parentheses in Evaluating Expressions Scanned with CamScanner operators and Expressions fore addition according to precedence rule, but if we enclose 2+4 ‘dition will be performed first, So the value of expression 24/ (264) is 4. ion inside parentheses same prec expression 24/244, division will take place be inside parentheses then lence and associativity rules apply. For example- Here inside parenthes S muultiptication will be performed before addition, There can be nesting of parentheses in expressions i.e. a pair of parentheses can be enclosed within another pair of parentheses. For example- (4* (342))/10 In these cases, expressions within innermost parentheses are always evaluated first, and then next innermost parentheses and so on, till outermost parentheses, After evaluation of all expressions within parentheses, the remaining expression is evaluated as usual. In the above expression 3+2 is evaluated first and then 45 and then 20/10. Sometimes in complex expressions, parentheses and spaces are used just to increase readability. For example ‘compare the following two expressions- x = al=begctd>emin x = (@tab) && ((e*d) >=(mtn)) These two expressions are evaluated in the same way but the operations performed in second one are clearer. Letus take some expressions and evaluate them according the precedence, associativity and parentheses rules. bed, c=2, de, es5, asb- (c+d) #3%e+£/9 a=17, b=5, c=6, d=3, e=5 at6-b/2+(c*d-5) /e arb-(c+d)e3te+ts9 atg-p/2+ (era -soe Lomt List L4ant 3rd ith Ist > rt 5 2 * a in 2nd 3rd 3 B 4 sth z 6h " 3 th 10 a4, be5, cx6, de3, ex5, £210 1 ba5, cx8, de}, e=65, £210, ga2, heS, k=2 atb-c/dee+t a-b+c/de-e/£-geh¥k as bee/ ade /f-gsnte atb -c/d< e+f » / / 3 ww | aa | [am | Lo | Lm | Loe len 2 5 3 2 6 1 sth on is 5 q % ny ‘ih : : 8th Scanned with CamScanner oT a=8, be3, cx2, de3, e92, fall a-bj | (a-btc) +dese-£43 as bil @-be ec) + dme-f The following program prints the results of above expressions- {24:12 Program to evaluate some expreseions+/ al int main(void) , 7 a8, bet, e=2, dal, eo5, £220; Printe("*d\t", ab” (cad) +38e+£/9) ; 17, be5, c-6, d-3, ec5; Printé ("¥d\t", a%6-b/2+(c#d-5) /e) ; ar4, beS, cx6, de3, en, £010; Print (*$d\t", atb-c/dcest) ; Serer aetna tae en5) fav gea) he5, a2; PEEREE("SA\E",a-bec/dane/t-gehthy a8, m2, d=3, €-2, fea, Print£ ("td\n" “>| | (a-btc) +deke-t43) , return 0; } Output: ws o 1 1 4.13 Order of Evaluation Of Operands does not specify the order in which th expression- in eee Y= (ox) + (oe, be adsl : 1 are (+432) and (=), t ands have (0 stapien the result has to be assigned to variable y. Before addition thse two oped i el a is ten Jill be assigned value a3 (ees), win second oma is better to avoid such Lype of expressions, “™M° 9(S+4). The eee el onder here are four operators where Cle 1 ar [osical ANDY c&:),ogical ORC ) condi aft . 1085 ier erites the order of evaluation of operands, These oP! on the lft side is evaluated fing, litional (2 ) and Comma operator , ). In all these cases the Scanned with CamScanner Operators and Expressions In && operator, if the first operand eval of | | operator if the first operand evalu: In the case of logical AND and logical OR o Exercise ca Peralors, sometimes there is no need to evaluate second operand, ates to false(zero), then second operand is not evaluated and in the case ates (0 true(non zero) then second operand is not evaluated, What is the output of the following programs; assume stdio.h is included in all programs. 1, int main(voia) 6 { printf (*td\n", a); return 0; int main (void) { int a: © = ach; @ = (ab) && (ced); printf (*c=4d, detd\n",c,d); return 0; *be1, 0,4; } int main(voia) int a=9,b=15,ce16,de12,e,£; e = !acb || bee); £ = (asb) 2? a-b: b-a; prince (rex return 0; 5 int main (void) return 0 } Ant main(void) int as5, bes; . Prine ("¥d, ¥d\t", +4a/b- Printé("4d,td\c",a,b) Printé("td, td\t", s4a,be+) 7 Printé ("¥d, td\n",a,b) ; return 0; } {tt pain ivotay int x,y,z; x = B44; Y = 49x44; z= (xey)--; Printé(*xetd, yetd, z=¥d\n") x,y) Scanned with CamScanner

You might also like