Prime

#### Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

#### Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

# Precedence and Associativity

## Operator precedence and associativity

• The sequence of operators and operands that reduces to a single value after the evaluation is called an expression
• If 2*3 is evaluated nothing great ,it gives 6 but if 2*3+2 is 6+2 =8 or 2*6=12.To avoid this confusion rules of `precedence and associativity` are used

Precedence: Operator precedence gives priorities to operators while evaluating an expression

Example: when 2*3+2 is evaluated  output is 8 but not 12 because the * operator is having more priority than + hence 2*3 is  evaluated first followed by 6+2

### Operator precedence table

• Operator precedence table gives the detail list of priorities for each and every operator
• Operators are listed from higher priority to lower
Precedence Operator Description Associativity
1 `::` Scope resolution Left-to-right
2 `++`   `--` Suffix/postfix increment and decrement
`type()` `type{}` Function-style typecast
`()` Function call
`[]` Array subscripting
`.` Element selection by reference
`->` Element selection through pointer
3 `++`   `--` Prefix increment and decrement Right-to-left
`+`   `-` Unary plus and minus
`!`   `~` Logical NOT and bitwise NOT
`(type)` C-style type cast
`*` Indirection (dereference)
`&` Address-of
`sizeof` Size-of
`new``new[]` Dynamic memory allocation
`delete``delete[]` Dynamic memory deallocation
4 `.*`   `->*` Pointer to member Left-to-right
5 `*`   `/`   `%` Multiplication, division, and remainder
6 `+`   `-` Addition and subtraction
7 `<<`   `>>` Bitwise left shift and right shift
8 `<`   `<=` For relational operators < and ≤ respectively
`>`   `>=` For relational operators > and ≥ respectively
9 `==`   `!=` For relational = and ≠ respectively
10 `&` Bitwise AND
11 `^` Bitwise XOR (exclusive or)
12 `|` Bitwise OR (inclusive or)
13 `&&` Logical AND
14 `||` Logical OR
15 `?:` Ternary conditional operator Right-to-left
`=` Direct assignment (provided by default for C++ classes)
`+=`   `-=` Assignment by sum and difference
`*=`   `/=`   `%=` Assignment by product, quotient, and remainder
`<<=`   `>>=` Assignment by bitwise left shift and right shift
`&=`   `^=`   `|=` Assignment by bitwise AND, XOR, and OR
16 `throw` Throw operator (for exceptions)
17 `,` Comma Left-to-right

#### Example1

Evaluate 5*4+(3+2)

• Parenthesis is having the highest priority
5*4+5
• Among * and +,* is having the highest priority
20 + 5= 25 is the final output

#### Example 2

Arithmetic operators for having higher priority than relational operators

Observe the difference With Parenthesis  and without Parenthesis

`5<4+5<4 5<9<4// 5<9 gives 1 1<4 =1`
`(5>4)+(5>4)//5>4 is true 1+12`

### Associativity

Associativity determines in which direction operators  are to be evaluated if they are under the same priority i.e

1. Left to Right
2. Right to Left

#### Example

5*3/2
15/2
7

Here * and / are having the same level of priority but it associativity is given as left to right. hence evaluate* followed by /

#### Note

From the Operator precedence table, we can conclude  except for   group 3 and group 15 all the remaining operators are having associativity as,`left to right`

#### C plus plus program to demonstrate operator precedence and associativity

`main() {int a = 20,b = 10,c = 15 d = 5,e;e = (a + b) * c / d; // ( 30 * 15 ) / 5cout << "Value of (a + b) * c / d is :" << e << endl ;e = ((a + b) * c) / d; // (30 * 15 ) / 5cout << "Value of ((a + b) * c) / d is :" << e << endl ;e = (a + b) * (c / d); // (30) * (15/5)cout << "Value of (a + b) * (c / d) is :" << e << endl ;e = a + (b * c) / d; // 20 + (150/5)cout << "Value of a + (b * c) / d is :" << e << endl ;}`

#### ​Using the rules of precedence and associativity evaluate the following expressions post your outputs and doubts in the comments

1. 5*3/2-1
2. (2+3)&&5+1/2
3. int a=2;
++a*++a*++a;
what is the value of’ a ‘after evaluating the above statement?