Skip to content

Commit

Permalink
add Associativity
Browse files Browse the repository at this point in the history
  • Loading branch information
nihalxkumar committed Apr 23, 2024
1 parent dc2877e commit db6e6c9
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Learning-C/Operators/Operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,32 @@ Similarly,`sizeof x + 1`is parsed as`(sizeof x) + 1`, since`sizeof`has higher pr
An expression like`p++->x`is parsed as`(p++)->x`; both postfix`++`and`->`operators have the same precedence, so they're
parsed from left to right.

When in doubt, use parentheses.
When in doubt, use parentheses.

### Associativity

Associativity refers to the order in which operators of the same precedence are evaluated in an expression.

There are two types of associativity: left associativity and right associativity.

1. **Left Associativity**:

* For operators with left associativity, evaluation proceeds from left to right. This means that if multiple operators of the same precedence appear consecutively in an expression, they are evaluated from left to right.

```c
int result = 10 - 5 + 2;
```

In this expression, both the subtraction (`-`) and addition (`+`) operators have the same precedence. Since they are left-associative, the subtraction operation `10 - 5` is evaluated first, followed by the addition operation `5 + 2`.

2. **Right Associativity**:

* Conversely, for operators with right associativity, evaluation occurs from right to left. This means that if multiple operators of the same precedence appear consecutively, they are evaluated from right to left.

```c
`int result = 2 ^ 3 ^ 2;`
```

In this expression, the bitwise XOR operator (`^`) has right associativity. Therefore, the evaluation starts from the rightmost operator `3 ^ 2`, and then the result is XORed with `2`.

Understanding associativity is crucial for correctly interpreting expressions and determining the order of operations. It helps in writing code that produces the expected results and avoids ambiguity in complex expressions.

0 comments on commit db6e6c9

Please sign in to comment.