Notes in Hindi

What is Operator Precedence in C Language in Hindi

/ BCA / Programming with C and CPP

Operator Precedence and Associativity in C Language in Hindi

What is Operator Precedence in C Language in Hindi

C Language में जब हम कोई expression लिखते हैं जिसमें एक से अधिक operators (जैसे +, *, /, - आदि) होते हैं, तो compiler को यह तय करना होता है कि सबसे पहले कौन सा operator काम करेगा। इस स्थिति में "Operator Precedence" यानी ऑपरेटर प्रेसीडेंस काम आता है।

Operator Precedence का मतलब है – किसी expression में मौजूद operators की प्राथमिकता। यानी किस operator को सबसे पहले execute किया जाएगा और किसे बाद में। जब एक ही expression में कई operators होते हैं, तो यह जानना जरूरी होता है कि कौन सा operator पहले चलेगा और कौन बाद में।

उदाहरण के लिए: int result = 10 + 20 * 5;

ऊपर दिए गए expression में दो operators हैं: + (addition) और * (multiplication)। अब सवाल यह है कि पहले addition होगा या multiplication? यहां Operator Precedence rule के अनुसार, * की precedence + से ज्यादा होती है, इसलिए पहले 20 * 5 होगा और फिर उस परिणाम में 10 जोड़ा जाएगा।

इसका मतलब: result = 10 + (20 * 5); // यानी result = 10 + 100 = 110

Why Operator Precedence is Important?

  • यह compiler को decide करने में मदद करता है कि पहले कौन सा operation perform किया जाएगा।
  • इससे expression का सही result निकलता है।
  • यह ambiguous यानी भ्रमित करने वाले situations को avoid करता है।

Meaning of Operator Associativity in C in Hindi

Associativity का मतलब होता है कि जब दो या दो से अधिक operators की precedence समान होती है, तो उन्हें किस क्रम (left to right या right to left) में execute किया जाएगा। इसे ही हम "Operator Associativity" कहते हैं।

मान लीजिए expression में दो operators हैं जिनकी precedence बराबर है, तो compiler associativity को follow करता है कि कौन सा पहले चलेगा।

Types of Associativity in C

  • Left to Right Associativity: इसका मतलब है कि operations बाएं से दाएं की ओर होंगे। उदाहरण: +, -, *, /, %
  • Right to Left Associativity: इसका मतलब है कि operations दाएं से बाएं की ओर होंगे। उदाहरण: =, +=, -=, sizeof

उदाहरण: int x = 10, y = 20, z = 30; int result = x - y - z;

यहाँ - operator की precedence बराबर है और associativity Left to Right है, तो expression ऐसे चलेगा: result = (x - y) - z; = (10 - 20) - 30; = -10 - 30; = -40;

अब एक और उदाहरण Right to Left Associativity का: int a; a = 10 + 20 + 30;

यहाँ assignment operator (=) की associativity Right to Left है लेकिन + की Left to Right होती है, इसलिए पहले addition होगा, फिर assignment।

Precedence table for C Operators in Hindi

नीचे C language के operators की precedence table दी गई है जो high से low precedence को दिखाती है:

Precedence Level Operators Associativity
1 (Highest) () , [] , . , -> Left to Right
2 ++ , -- , + (Unary), - (Unary), !, ~ Right to Left
3 * , / , % Left to Right
4 + , - Left to Right
5 << , >> Left to Right
6 < , <= , > , >= Left to Right
7 == , != Left to Right
8 & Left to Right
9 ^ Left to Right
10 | Left to Right
11 && Left to Right
12 || Left to Right
13 ?: Right to Left
14 =, +=, -=, *=, /=, %= Right to Left
15 (Lowest) , Left to Right

Examples showing Precedence and Associativity rules in Hindi

Example 1: Precedence

int result = 10 + 5 * 2; // Output: 10 + (5 * 2) = 10 + 10 = 20

यहाँ * की precedence + से अधिक है, इसलिए पहले multiplication होगा फिर addition।

Example 2: Associativity

int result = 100 / 10 * 2; // Output: (100 / 10) * 2 = 10 * 2 = 20

यहाँ / और * की precedence same है, इसलिए associativity Left to Right के अनुसार पहले division हुआ फिर multiplication।

Example 3: Using Parentheses

int result = (10 + 5) * 2; // Output: (15) * 2 = 30

जब हम parentheses का उपयोग करते हैं तो हम manually precedence set कर सकते हैं, जो कि highly recommended होता है complex expressions में।

Example 4: Right to Left Associativity

int a = 5; int b = 10; int c = 15; a = b = c; // Output: b = 15, a = 15

Assignment operator (=) की associativity Right to Left होती है, इसलिए सबसे पहले b = c execute हुआ और फिर a = b.

Example 5: Complex Expression

int result = 2 + 3 * 4 - 5; // Output: 2 + (3 * 4) - 5 = 2 + 12 - 5 = 9

Operator precedence: * > + = - Associativity: Left to Right इसलिए पहले multiplication, फिर addition और फिर subtraction हुआ।

Key Takeaways:

  • Operator Precedence यह तय करती है कि पहले कौन सा operator execute होगा।
  • Operator Associativity यह तय करती है कि बराबर precedence वाले operators को किस क्रम में चलाया जाएगा।
  • Parentheses का उपयोग कर के हम precedence को override कर सकते हैं।
  • Code समझने और bugs से बचने के लिए हमेशा parentheses का सही उपयोग करें।

FAQs

Operator Precedence का मतलब होता है कि किसी expression में पहले कौन सा operator execute होगा। C Language में जब एक से अधिक operators एक साथ expression में होते हैं, तो precedence के नियम से compiler तय करता है कि कौन सा operator पहले चलेगा और कौन बाद में।
जब दो या अधिक operators की precedence समान होती है, तब उन्हें किस direction (left to right या right to left) में execute किया जाएगा, यह Associativity तय करती है। उदाहरण के लिए, + और - की associativity left to right होती है।
C में सबसे अधिक precedence () (parentheses), [] (array subscript), . (dot) और -> (arrow) operators की होती है। ये सभी सबसे पहले evaluate होते हैं।
जब एक expression में एक ही precedence के कई operators हों, तो associativity यह तय करती है कि उन्हें किस क्रम में evaluate किया जाएगा। इससे final result बदल सकता है। जैसे assignment operators की associativity right to left होती है।
C में operator precedence को parentheses () की मदद से बदला जा सकता है। जब हम किसी expression में किसी हिस्से को () में डालते हैं, तो वह हिस्सा सबसे पहले evaluate होता है, भले ही उसकी precedence कम क्यों न हो।

Please Give Us Feedback