Operator Precedence and Associativity in C Programming in Hindi

इस tutorial में हम C Programming में operator precedence and associativity के बारे में बात करेंगे और साथ ही इससे related कुछ examples भी देखेंगे जिससे ये topic आपको अच्छे से समझ आए.

आप सभी ने math में BODMAS rule तो पढ़ा ही होगा जिसका use करके हम expression (e.g.  12 + 5 x 13 / 2 – 1) को solve करते वक्त operations order decide करते हैं.

ठीक इसी तरह C programming में भी expression को evaluate करते वक्त operator precedence and associativity के rules को follow करना पड़ता है.

Operator Precedence in C Language

जब किसी expression में एक से ज्यादा operators use होते हैं तब operator precedence के द्वारा सभी operators को priorities दी जाती है.

Operators priorities के आधार पर ही ये तय किया जाता है की expression को evaluate करते वक्त किस operator को पहले solve किया जाएगा. आइए example के साथ समझते हैं.

2 + 5 * 3 = 7 * 3 = 21 (wrong)
2 + 5 * 3 = 2 + 15 = 17 (right)

अगर हम  operator precedence के rules को बिना follow करें expression को solve करेंगे तो हमारा output भी ऊपर दिए first example की तरह गलत ही आएगा.

क्योंकि C language में multiply ( * ) की operator precedence addition ( + ) की operator precedence से ज्यादा होती है.

इसलिए पहले 5 * 3 को evaluted किया जाएगा उसके बाद उसमे 2 को add किया जाएगा तब जाकर आपको second example की तरह सही output मिलेगा. आइए एक और example समझते हैं.

(2 + 5) * 3 = 7 * 3 = 21

ऊपर वाले expression में हमने 2 + 5 को parentheses ( ) के अंदर रख दिया और parentheses की operator precedence * operator से ज्यादा होती है. 

इसलिए पहले 2 + 5 को evaluted किया जाएगा उसके बाद उसमे 3 को multiply किया जाएगा तब जाकर आपको सही output मिलेगा.

Operator Associativity in C Language

जब किसी expression में same precedence वाले एक से ज्यादा operators use होते हैं तब operator associativity के द्वारा उन operators को evaluate करने का order (left to right or right to left) तय किया जाता है. आइए example के साथ समझते हैं.

5 * 3 + 8 / 2

ऊपर वाले expression में * और  / की operator precedence (priority) same होती है इसलिए अब यहाँ हम इन operators की associativity देखकर इस  expression को solve करेंगे. 

नीचे दी गयी table में C operator precedence and associativity की list दी है जिसे आप ध्यानपूर्वक देखें और याद रखें.

Operator Precedence and Associativity Table in C Programming

Operator Precedence and Associativity
Operator Description Precedence Associativity
( ) Parentheses 1 Left to right
[ ] Array element reference
-> Member access via pointer
. Member access via object name
++ Postfix increment
Postfix decrement
! Logical NOT operator 2 Right to left
~ Bitwise complement operator
+ Unary plus operator
Unary minus operator
++ Prefix Increment
Prefix Decrement
* Indirection operator
sizeof() Size of operator
(type) Type cast
.* Dereference operator 3 Left to right
->* Dereference operator
* Multiply 4 Left to right
/ Divide
% Modulus
+ Binary addition 5 Left to right
Binary subtraction
<< Bitwise left shift 6 Left to right
>> Bitwise right shift
< Less than 7 Left to right
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to 8 Left to right
!= Not equal to
& Bitwise AND 9 Left to right
^ Bitwise XOR 10 Left to right
| Bitwise OR 11 Left to right
&& Logical AND 12 Left to right
|| Logical OR 13 Left to right
?: Conditional operator 14 Right to left
= Simple assignment 15 Right to left
*= Assign product
~= Assign bitwise complement
/= Assign quotient
%= Assign remainder
+= Assign sum
-= Assign difference
&= Assign bitwise AND
^= Assign bitwise XOR
|= Assign bitwise OR
<<= Assign bitwise left shift
>>= Assign bitwise right shift
, Comma operator 16 Left to right

What’s Next: इस tutorial में हमने C Operators Precedence and Associativity के बारे में पढ़ा. Next tutorial में हम C programming में Type Casting करना सीखेंगे.