2479 views|1 replies

3836

Posts

19

Resources
The OP
 

Operator precedence and associativity in C language [Copy link]

If an expression contains two or more operators, the way the operands are combined determines the result of the expression. For simplicity, we call expressions containing two or more operators compound expressions. For example: 3 + 2 * 5 If 3 and 2 are combined, the result of this expression will be 5 * 5, which equals 25. Of course, in C, 3 is not combined with 2 in this example, but 2 is combined with 5, resulting in 3 + 10, which equals 13. The precedence and associativity of operators determine how the operands are combined. When the operators in a compound expression have different precedences, the precedence determines how the operands are combined. When the operators in a compound expression have the same precedence, the associativity determines how the operands are combined. However, we can also use parentheses to force the operands to be combined. For example:(3 + 2) * 5forces 3 and 2 to be combined.1. PrecedenceThe way the operands are combined determines the value of a compound expression. When the operators in a compound expression have different precedence, the precedence determines how the operands are combined. Operands are always combined around the operator with the higher precedence. For example:8.0 + 20.0 / 4.0Because the division operator has a higher precedence than the addition operator, 20.0 is combined with 4.0 to get 5.0, and then 8.0 is combined with 5.0 to get 13.0.2. When the operators in a compound expression have the same precedence, the associativity determines how the operands are combined. For example:8.0 + 20.0 / 4.0 * 2.0The multiplication operator has the same precedence as the division operator. However, since they are associative from left to right (i.e., they associate from left to right), 20.0 is combined with 4.0; the result, 5.0, is combined with 2.0 to get 10.0; and 8.0 is combined with 10.0 to get 18.0.Most operators are associative from left to right, but some are associative from right to left (such as assignment operators).3. Use parentheses to force operands to be combinedUse parentheses to force operands to be combined. The subexpression enclosed in parentheses is treated as an independent entity, which is also subject to the constraints of precedence and associativity. For example: (8.0 + 20.0 / 4.0) * 2.0 forces 8.0 + 20.0 / 4.0 to be treated as an individual. Because the division operator has a higher precedence than the addition operator, 20.0 and 4.0 are combined to get 5.0; then 8.0 and 5.0 are combined to get 13.0; 13.0 and 2.0 are combined to get 26.0. -9 + 4 * 5 + 6 + 8 * 7 - 1 According to precedence and associativity, the associativity of this expression is equivalent to: ( ( ( ( -9) + (4 * 5) ) + 6 ) + (8 * 7) ) - 1 ) Since the associativity of the assignment operator is from right to left, the associativity of I = J = K = L is equivalent to: ( I = ( J = (K = L) ) ) 4. Order of Operations Precedence and associativity can only determine the way operands are combined, but cannot determine the order in which the operands are operated. For example: 5 * 3 + 8 * 4 According to precedence and associativity, we know that the way this expression is combined is equivalent to (5 * 3) + (8 * 4) // 5 * 3 and 8 * 4 are the operands of + However, we cannot be sure whether 5 * 3 or 8 * 4 is operated first. The order in which they are operated is determined by the compiler. The standard does not specify the order in which operands are operated in order to allow the compiler of a specific system to choose the most efficient order of operations for that system. But no matter how they are calculated, the final result is 47. Let's take a look at a small program. #Include Int Main(Void) { [size=4 ] Int Var1, Var2; Var1 = Var2 = -(9 + 4) * 5 + (6 + 8 * (7- 1)); Printf("Var1 = Var2 = %D ", Var1); [ size=4] Return 0; } Please read the above program carefully, think about what results will appear, and then compile and run it to see if the results are what you expected. ]Because precedence and associativity cannot determine the order of operations on operands, we do not know whether -(9 + 4) * 5 or (6 + 8 * (7 - 1)) is operated first. It is determined by the compiler. Assume that -(9 + 4) * 5 is calculated first. Although the addition operator has a lower priority, the parentheses force 9 and 4 to be combined together to get 13. The priority of the minus operator Higher than the multiplication operator, it is applied to 13 and gives -13. So we get: Var1 = Var2 = -13 * 5 + (6 + 8 * (7 - 1)); 4] Since the multiplication operator has higher precedence than the addition operator, -13 and 5 are combined to get -65: Var1 = Var2 = -65 + (6 + 8 * (7 - 1)); - 1)) In the parentheses, 7 and 1 are forced to combine, resulting in 6: Var1 = Var2 = -65 + (6 + 8 * 6); Because* has a higher priority than +, so we get: Var1 = Var2 = -65 + (6 + 48); Furthermore Var1 = Var2 = -65 + 54; Var1 = Var2 = -11; Because the associativity of the assignment operator is from right to left, -11 is assigned to Var2. , and then Var2 is assigned to Var1. The final result is that Var1 and Var2 are equal, and their values are both -11. 5. Operators that specify the order of operations on operands C language In , there are four operators that clearly define the order of operation of their operands: && || ?: , [size =4] Except for these four operators, other operators do not specify the order of operation of their operands.

This post is from Microcontroller MCU

Latest reply

I usually use parentheses to calculate the priority, so I don't have to bother remembering which one has a higher priority and which one has a lower priority, and parentheses are the safest  Details Published on 2018-12-25 17:59
 

6366

Posts

4937

Resources
2
 
I usually use parentheses to calculate the priority, so I don't have to bother remembering which one has a higher priority and which one has a lower priority, and parentheses are the safest
This post is from Microcontroller MCU
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list