You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/c-language/precedence-and-order-of-evaluation.md
+15-19Lines changed: 15 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -20,16 +20,16 @@ The precedence and associativity of C operators affect the grouping and evaluati
20
20
21
21
The following table summarizes the precedence and associativity (the order in which the operands are evaluated) of C operators, listing them in order of precedence from highest to lowest. Where several operators appear together, they have equal precedence and are evaluated according to their associativity. The operators in the table are described in the sections beginning with [Postfix Operators](../c-language/postfix-operators.md). The rest of this section gives general information about precedence and associativity.
22
22
23
-
###Precedence and Associativity of C Operators
23
+
## Precedence and Associativity of C Operators
24
24
25
-
|Symbol1|Type of Operation|Associativity|
25
+
|Symbol <sup>1</sup>|Type of Operation|Associativity|
@@ -38,7 +38,7 @@ The precedence and associativity of C operators affect the grouping and evaluati
38
38
|**&&**|Logical-AND|Left to right|
39
39
|**||**|Logical-OR|Left to right|
40
40
|**? :**|Conditional-expression|Right to left|
41
-
|**= \*= /= %=**<br /><br /> **+= -= <\<= >>=&=**<br /><br /> **^= |=**|Simple and compound assignment2|Right to left|
41
+
|**= \*= /= %=**<br /><br /> **+= -= \<\<= >>=&=**<br /><br /> **^= |=**|Simple and compound assignment <sup>2</sup>|Right to left|
42
42
|**,**|Sequential evaluation|Left to right|
43
43
44
44
1. Operators are listed in descending order of precedence. If several operators appear on the same line or in a group, they have equal precedence.
@@ -51,22 +51,20 @@ The precedence and associativity of C operators affect the grouping and evaluati
51
51
52
52
Logical operators also guarantee evaluation of their operands from left to right. However, they evaluate the smallest number of operands needed to determine the result of the expression. This is called "short-circuit" evaluation. Thus, some operands of the expression may not be evaluated. For example, in the expression
53
53
54
-
```
55
-
x && y++
56
-
```
54
+
`x && y++`
57
55
58
56
the second operand, `y++`, is evaluated only if `x` is true (nonzero). Thus, `y` is not incremented if `x` is false (0).
59
57
60
-
**Examples**
58
+
## Examples
61
59
62
60
The following list shows how the compiler automatically binds several sample expressions:
63
-
61
+
64
62
|Expression|Automatic Binding|
65
63
|----------------|-----------------------|
66
-
|`a & b || c`|`(a & b) || c`|
67
-
|`a = b || c`|`a = (b || c)`|
68
-
|`q && r || s--`|`(q && r) || s--`|
69
-
64
+
|a & b || c|(a & b) || c|
65
+
|a = b || c|a = (b || c)|
66
+
|q && r || s--|(q && r) || s--|
67
+
70
68
In the first expression, the bitwise-AND operator (`&`) has higher precedence than the logical-OR operator (`||`), so `a & b` forms the first operand of the logical-OR operation.
71
69
72
70
In the second expression, the logical-OR operator (`||`) has higher precedence than the simple-assignment operator (`=`), so `b || c` is grouped as the right-hand operand in the assignment. Note that the value assigned to `a` is either 0 or 1.
@@ -77,13 +75,11 @@ x && y++
77
75
78
76
|Illegal Expression|Default Grouping|
79
77
|------------------------|----------------------|
80
-
|`p == 0 ? p += 1: p += 2`|`( p == 0 ? p += 1 : p ) += 2`|
78
+
|p == 0 ? p += 1: p += 2|( p == 0 ? p += 1 : p ) += 2|
81
79
82
80
In this expression, the equality operator (`==`) has the highest precedence, so `p == 0` is grouped as an operand. The conditional-expression operator (`? :`) has the next-highest precedence. Its first operand is `p == 0`, and its second operand is `p += 1`. However, the last operand of the conditional-expression operator is considered to be `p` rather than `p += 2`, since this occurrence of `p` binds more closely to the conditional-expression operator than it does to the compound-assignment operator. A syntax error occurs because `+= 2` does not have a left-hand operand. You should use parentheses to prevent errors of this kind and produce more readable code. For example, you could use parentheses as shown below to correct and clarify the preceding example:
0 commit comments