Precedence and Associativity of operators in c




Precedence and Associativity of Operators in C




Table of Contents



  1. Introduction

  2. Operator Precedence and Associativity

  3. Precedence and Associativity Table


  4. Examples and Programs

    1. Example 1: Arithmetic Operations

    2. Example 2: Logical Operations

    3. Example 3: Bitwise Operations



  5. Conclusion




1. Introduction



In the C programming language, operators are symbols that perform operations on operands.
When multiple operators are used in an expression, their order of evaluation can affect the final result.
The precedence and associativity of operators define the rules for evaluating expressions containing multiple operators.
Understanding these rules is crucial to writing correct and efficient C programs.




2. Operator Precedence and Associativity



Operator Precedence: Precedence determines the order in which operators are evaluated in an expression.
Operators with higher precedence are evaluated before those with lower precedence.
For example, in the expression 2 + 3 * 4, the multiplication operation has higher precedence, so it is performed first, resulting in the evaluation 2 + 12.



Operator Associativity: Associativity defines the order in which operators with the same precedence are evaluated.
Operators can be left-associative or right-associative.
In left-associative operators, evaluation proceeds from left to right, while in right-associative operators, evaluation proceeds from right to left.




3. Precedence and Associativity Table



































































































Precedence Operator Description Associativity
1 () [] -> . Parentheses, Brackets, Members Left to Right
2 ++ -- ~ ! Increment, Decrement, Complement, Logical NOT Right to Left
3 * / % Multiplication, Division, Modulo Left to Right
4 + - Addition, Subtraction Left to Right
5 << >> Bitwise Left Shift, Right Shift Left to Right
6 < <= > >= Relational Operators Left to Right
7 == != Equality Operators Left to Right
8 & Bitwise AND Left to Right
9 ^ Bitwise XOR Left to Right
10 | Bitwise OR Left to Right
11 && Logical AND Left to Right
12 || Logical OR Left to Right
13 ?: Conditional Operator Right to Left
14 = += -= *= /= %= <<= >>= &= ^= |= Assignment Operators Right to Left
15 , Comma Operator Left to Right



4. Examples and Programs




4.1 Example 1: Arithmetic Operations


#include <stdio.h>

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

result = a + b * c; // Multiplication has higher precedence
printf("Result: %d\\n", result); // Output: Result: 11

result = (a + b) * c; // Parentheses change the evaluation order
printf("Result: %d\\n", result); // Output: Result: 21

return 0;
}



4.2 Example 2: Logical Operations


#include <stdio.h>

int main() {
int x = 1, y = 0, z = 1;
int result;

result = x && y || z; // Logical AND has higher precedence
printf("Result: %d\\n", result); // Output: Result: 1

result = x || y && z; // Logical AND has higher precedence, but evaluated first due to associativity
printf("Result: %d\\n", result); // Output: Result: 1

return 0;
}



4.3 Example 3: Bitwise Operations


#include <stdio.h>

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

result = a & b << c; // Bitwise Left Shift has higher precedence
printf("Result: %d\\n", result); // Output: Result: 4

result = (a & b) << c; // Parentheses change the evaluation order
printf("Result: %d\\n", result); // Output: Result: 0

return 0;
}



5. Conclusion



In C programming, understanding the precedence and associativity of operators is crucial for writing correct and efficient code.
It ensures that expressions are evaluated in the desired order and leads to predictable results.
By referring to the provided table and examples, you can confidently use operators in your C programs while adhering to the correct evaluation rules.
Happy coding!





Post a Comment

0 Comments