Operator Precedence and Associativity in C Language with Examples

In C language, an expression may contain multiple operators. Operator Precedence and Associativity determine the order in which operators are grouped and evaluated in an expression. Operator Precedence determines which operator has higher priority, whereas Associativity determines the grouping direction when operators have the same precedence.

C भाषा में किसी Expression में एक से अधिक Operators हो सकते हैं। Operator Precedence तथा Associativity यह निर्धारित करते हैं कि Expression में Operators को किस क्रम में Group किया जाएगा और उनका मूल्यांकन किस प्रकार होगा। Operator Precedence यह निर्धारित करता है कि किस Operator की Priority अधिक है, जबकि Associativity समान Precedence वाले Operators के Grouping की दिशा निर्धारित करती है।

1. Operator Precedence (ऑपरेटर प्राथमिकता)

Operator Precedence defines the priority of operators in an expression. An operator with higher precedence is grouped before an operator with lower precedence.

Operator Precedence किसी Expression में Operators की Priority निर्धारित करता है। जिस Operator की Precedence अधिक होती है, उसे कम Precedence वाले Operator से पहले Group किया जाता है।

Example:

#include <stdio.h>
int main(void)
{
int result;
result = 10 + 5 * 2;
printf("Result = %d\n", result);
return 0;
}

Output:

Result = 20

Explanation (व्याख्या):

In this expression:

10 + 5 * 2

Multiplication (*) has higher precedence than Addition (+).

Therefore:

  • First, 5 * 2 = 10

  • Then, 10 + 10 = 20

इस Expression में Multiplication (*) की Precedence Addition (+) से अधिक होती है।

इसलिए:

  • सबसे पहले 5 * 2 = 10 होगा।

  • इसके बाद 10 + 10 = 20 होगा।

Important: Parentheses can be used to change the grouping of an expression.

महत्वपूर्ण: Parentheses () का उपयोग Expression की Grouping बदलने के लिए किया जा सकता है।

For example:

result = (10 + 5) * 2;

Output:

Result = 30

Here, the expression inside parentheses is grouped first.

यहाँ Parentheses के अंदर वाले Expression को पहले Group किया जाता है।

2. Operator Associativity (ऑपरेटर साहचर्य)

Associativity determines how operators with the same precedence are grouped when they appear together in an expression.

Associativity यह निर्धारित करती है कि समान Precedence वाले Operators एक Expression में साथ आने पर किस दिशा में Group होंगे।

There are two types of associativity:

  1. Left-to-Right Associativity

  2. Right-to-Left Associativity

Associativity के दो प्रकार होते हैं:

  1. Left-to-Right Associativity

  2. Right-to-Left Associativity

A. Left-to-Right Associativity

When operators have the same precedence and left-to-right associativity, they are grouped from left to right.

जब समान Precedence वाले Operators की Associativity Left-to-Right होती है, तो उन्हें बाएँ से दाएँ Group किया जाता है।

Example:

#include <stdio.h>
int main(void)
{
int result;
result = 20 - 5 - 3;
printf("Result = %d\n", result);
return 0;
}

Output:

Result = 12

Explanation:

Subtraction (-) has left-to-right associativity.

Therefore:

20 - 5 - 3

(20 - 5) - 3

15 - 3 = 12

Subtraction (-) की Associativity Left-to-Right होती है।

इसलिए पहले 20 - 5 किया जाएगा और उसके बाद प्राप्त Result में से 3 घटाया जाएगा।

B. Right-to-Left Associativity

When operators have the same precedence and right-to-left associativity, they are grouped from right to left.

जब समान Precedence वाले Operators की Associativity Right-to-Left होती है, तो उन्हें दाएँ से बाएँ Group किया जाता है।

Example:

#include <stdio.h>
int main(void)
{
int a, b, c;
a = b = c = 10;
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("c = %d\n", c);
return 0;
}

Output:

a = 10
b = 10
c = 10

Explanation:

The assignment operator (=) has right-to-left associativity.

Therefore:

a = (b = (c = 10))

First, 10 is assigned to c, then to b, and finally to a.

Assignment Operator (=) की Associativity Right-to-Left होती है।

इसलिए पहले 10 को c में, फिर b में और अंत में a में Assign किया जाता है।

3. Operator Precedence and Associativity Table in C

The following table shows the commonly used C operators in descending order of precedence.

निम्न Table में C भाषा के सामान्य Operators को उनकी Precedence के घटते क्रम में दर्शाया गया है।

PriorityOperatorsDescriptionAssociativity
1 (Highest)(), [], ., ->, expr++, expr--Postfix, function call, array and member accessLeft to Right
2++, --, +, -, !, ~, *, &, sizeof, (type)Unary, cast and sizeofRight to Left
3*, /, %Multiplication, division, modulusLeft to Right
4+, -Addition, subtractionLeft to Right
5<<, >>Bitwise shiftLeft to Right
6<, <=, >, >=RelationalLeft to Right
7==, !=EqualityLeft to Right
8&Bitwise ANDLeft to Right
9^Bitwise XORLeft to Right
10|Bitwise ORLeft to Right
11&&Logical ANDLeft to Right
12||Logical ORLeft to Right

13?:ConditionalRight to Left
14=, +=, -=, *=, /=, %= and other assignment operatorsAssignmentRight to Left
15 (Lowest),Comma operatorLeft to Right

Note: This table shows the main operator groups. Operators within the same row have the same precedence. The comma operator shown in the last row is different from commas used merely as separators in function arguments or declarations.

ध्यान दें: यह Table मुख्य Operator Groups को दर्शाती है। एक ही Row में दिए गए Operators की Precedence समान होती है। अंतिम Row में दिया गया Comma Operator उन Commas से अलग है जिनका उपयोग केवल Function Arguments या Declarations को अलग करने के लिए किया जाता है।

4. Examples of Operator Precedence

Example 1: Arithmetic Operators

#include <stdio.h>
int main(void)
{
int result;
result = 10 + 20 / 5 * 2;
printf("Result = %d\n", result);
return 0;
}

Output:

Result = 18

Explanation:

Division (/) and multiplication (*) have higher precedence than addition (+). They have the same precedence and are grouped from left to right.

20 / 5 = 4

4 * 2 = 8

10 + 8 = 18

Division (/) तथा Multiplication (*) की Precedence Addition (+) से अधिक होती है। दोनों की Precedence समान है तथा इन्हें Left-to-Right Group किया जाता है।

Example 2: Relational and Logical Operators

#include <stdio.h>
int main(void)
{
int a = 10, b = 5, c = 20;
int result;
result = a > b && c > a;
printf("Result = %d\n", result);
return 0;
}

Output:

Result = 1

Explanation:

Relational operators (>) have higher precedence than Logical AND (&&).

Both conditions are true:

  • 10 > 5 gives 1.

  • 20 > 10 gives 1.

  • 1 && 1 gives 1.

Relational Operators (>) की Precedence Logical AND (&&) से अधिक होती है।

दोनों Conditions True हैं, इसलिए अंतिम Result 1 प्राप्त होता है।

Example 3: Assignment Operator

#include <stdio.h>
int main(void)
{
int a, b, c;
a = b = c = 5;
printf("%d %d %d\n", a, b, c);
return 0;
}

Output:

5 5 5

Explanation: Assignment operators have right-to-left associativity.

व्याख्या: Assignment Operators की Associativity Right-to-Left होती है।

Example 4: Conditional Operator

#include <stdio.h>
int main(void)
{
int a = 10, b = 20, result;
result = (a > b) ? a : b;
printf("Greater number = %d\n", result);
return 0;
}

Output:

Greater number = 20

Explanation: The condition is false, so the second expression (b) is selected.

व्याख्या: Condition False है, इसलिए दूसरा Expression (b) Select किया जाता है।

5. Important Difference Between Precedence and Associativity

(Precedence तथा Associativity में अंतर)

Operator PrecedenceOperator Associativity
Determines the priority of operators.Determines grouping direction for operators of equal precedence.
It decides which operator binds more tightly.It decides grouping from left to right or right to left.
यह Operators की Priority निर्धारित करता है।यह समान Precedence वाले Operators की Grouping की दिशा निर्धारित करता है।
Example: * has higher precedence than +.Example: 20 - 5 - 3 is grouped from left to right.

6. Important Points for Examination

(परीक्षा के लिए महत्वपूर्ण बिंदु)

  1. Multiplication, Division and Modulus have higher precedence than Addition and Subtraction.

    Multiplication, Division तथा Modulus की Precedence Addition तथा Subtraction से अधिक होती है।

  2. Relational operators have higher precedence than Logical AND (&&) and Logical OR (||).

    Relational Operators की Precedence Logical AND (&&) तथा Logical OR (||) से अधिक होती है।

  3. Logical AND (&&) has higher precedence than Logical OR (||).

    Logical AND (&&) की Precedence Logical OR (||) से अधिक होती है।

  4. Assignment operators have right-to-left associativity.

    Assignment Operators की Associativity Right-to-Left होती है।

  5. The Conditional Operator (?:) has right-to-left associativity.

    Conditional Operator (?:) की Associativity Right-to-Left होती है।

  6. Parentheses () can be used to control the grouping of expressions.

    Parentheses () का उपयोग Expressions की Grouping को नियंत्रित करने के लिए किया जा सकता है।

  7. Precedence and associativity determine grouping, not necessarily the actual order in which every operand or subexpression is evaluated. C also has sequencing rules.

    Precedence तथा Associativity Expression की Grouping निर्धारित करते हैं, लेकिन आवश्यक नहीं कि वे प्रत्येक Operand या Subexpression के वास्तविक Evaluation Order को भी निर्धारित करें। C भाषा में Sequencing के अलग नियम भी होते हैं।

Operator Precedence and Associativity are important concepts in C programming. They help programmers understand how expressions are grouped and how operators interact with each other. A clear understanding of these concepts is essential for writing correct expressions and solving output-based questions in competitive examinations.

Operator Precedence तथा Associativity C Programming की महत्वपूर्ण Concepts हैं। ये समझने में सहायता करती हैं कि Expressions में Operators किस प्रकार Group होते हैं तथा एक-दूसरे के साथ कैसे कार्य करते हैं। सही Expressions लिखने तथा Competitive Examinations के Output-Based Questions हल करने के लिए इन Concepts का स्पष्ट ज्ञान आवश्यक है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025