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

Operator Precedence and Associativity in C

Operator precedence in C determines the order of operations in expressions, while associativity defines how operators of the same precedence are grouped. Parentheses can override precedence, ensuring specific evaluation order. A C program example demonstrates the calculation of an expression, showing that using parentheses changes the evaluation sequence to yield a correct result.

Uploaded by

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

Operator Precedence and Associativity in C

Operator precedence in C determines the order of operations in expressions, while associativity defines how operators of the same precedence are grouped. Parentheses can override precedence, ensuring specific evaluation order. A C program example demonstrates the calculation of an expression, showing that using parentheses changes the evaluation sequence to yield a correct result.

Uploaded by

dacim55835
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

What is the importance of precedence and associativity?

Write the table for operator


precedence (July 2021)

Write a C program that calculates and prints the result of the following expression:
result = 5 * (6 + 2) / 4 - 3. Explain how parentheses affect the precedence of operators in
this expression.(Model Question – 2024 Scheme)

Operator Precedence and Associativity in C

In C, operator precedence determines the order in which operators are evaluated in an


expression. Associativity defines how operators of the same precedence level are grouped
together—either left-to-right or right-to-left.

1. Operator Precedence in C

Operator precedence determines which operation is performed first when there are multiple
operators in an expression. Operators with higher precedence are evaluated before those
with lower precedence.

Precedence Table (Highest to Lowest)


Example 1: Multiplication vs Addition

int x = 15 + 20 * 3;
// '*' has higher precedence than '+', so evaluated as: 15 + (20 * 3) = 75

Parentheses Affect Precedence

In C, parentheses ( ) override operator precedence, ensuring that the enclosed expression is


evaluated first, regardless of the default precedence rules.

int y = (15 + 20) * 3;


// Parentheses override precedence: (15 + 20) * 3 = 105

2. Operator Associativity

If multiple operators of the same precedence level appear in an expression, associativity


determines the direction in which the operations are performed.

Types of Associativity

1. Left-to-Right Associativity: Operators at the same level are evaluated from left to right.
2. Right-to-Left Associativity: Operators at the same level are evaluated from right to left.

Example: Left-to-Right Associativity

int result = 10 - 4 - 2;
// Evaluated as: (10 - 4) - 2 = 4

Subtraction follows left-to-right associativity.


Example: Right-to-Left Associativity

int a = 10;
int b = 20;
int c = 30;
a = b = c; // Right-to-left: a = (b = c) → b gets 30, then a gets 30

Assignment (=) follows right-to-left associativity.

3. Examples of Operator Precedence and Associativity


#include <stdio.h>

int main() {
int a = 5, b = 10, c = 2, result;

result = a + b * c / 2 - 1 && b > c || a == c;

printf("Result: %d\n", result);


return 0;
}

// Evaluate
int a = 5, b = 10, c = 2, result;

result = a + b * c / 2 - 1 && b > c || a == c;

Multiplication (*) and Division (/) (Higher precedence, Left-to-Right)


b * c / 2 → 10 * 2 / 2 → 20 / 2 → 10

result = a + 10 - 1 && b>c || a == c

Addition (+) and Subtraction (-) (Left-to-Right)


a + 10 - 1 → 5 + 10 - 1 → 15 - 1 → 14

result = 14 && b>c || a == c

Relational Operator (b > c) (Higher than logical operators, Left-to-Right)


b > c → 10 > 2 → 1 (true)

result = 14 && 1 || a == c

Logical AND (&&) (Higher than ||, Left-to-Right)


14 && 1 → 1

result = 1 || a == c
Equality Operator (a == c)
a == c → 5 == 2 → 0 (false)

result = 1 || 0

Logical OR (||) (Lowest precedence, Left-to-Right)


1 || 0 → 1

Final output , result = 1

4. How to Control Precedence and Associativity

1. Use Parentheses () to explicitly define evaluation order.


2. Be aware of associativity when working with operators of the same precedence.
3. Avoid complex expressions that rely on precedence rules.

5. Summary

 Operator precedence determines which operation is performed first.


 Associativity determines whether operators at the same level are evaluated left-to-right or
right-to-left.
 Parentheses can override precedence to ensure correct evaluation order.

Write a C program that calculates and prints the result of the following expression:
result = 5 * (6 + 2) / 4 - 3. Explain how parentheses affect the precedence of operators in
this expression.(Model Question – 2024 Scheme)

#include <stdio.h>

int main() {
int result;

// Calculating the expression


result = 5 * (6 + 2) / 4 - 3;

// Printing the result


printf("Result: %d\n", result);

return 0;
}
Parentheses can override precedence to ensure correct evaluation order.

Step-by-Step Calculation:

1. Parentheses first: 6+2=8


2. Multiplication (*) next: 5×8=40
3. Division (/) follows: 40÷4=10
4. Subtraction (-) last: 10−3=7

✅ Final Output:

Result: 7

You might also like