Operator Precedence and Associativity Rules in C Notes in Hindi & English : ऑपरेटर प्राथमिकता (Precedence) और साहचर्य (Associativity) नियम और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Operator Precedence and Associativity Rules in C (ऑपरेटर प्राथमिकता (Precedence) और साहचर्य (Associativity) नियम) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।

Overview: Operator precedence and associativity rules in C define how expressions with multiple operators are syntactically grouped across 15 precedence tiers. Crucially, precedence controls expression parsing (the syntax tree), NOT the runtime evaluation order of operands in expressions like f() + g() * h().

अध्याय सार: C में ऑपरेटर प्राथमिकता (Precedence) और साहचर्य (Associativity) नियम 15 स्तरों में यह तय करते हैं कि जटिल एक्सप्रेशन के ऑपरेंड आपस में कैसे जुड़ेंगे। ध्यान रहे कि प्राथमिकता केवल सिंटैक्स ग्रुपिंग तय करती है, न कि `f() + g() * h()` जैसे सब-एक्सप्रेशंस के रनटाइम चलने का क्रम।

Operator Precedence and Associativity Rules in C: Detailed Notes & Architecture (ऑपरेटर प्राथमिकता (Precedence) और साहचर्य (Associativity) नियम)

1. The 15-Tier Precedence Hierarchy & Associativity Tie-Breaking (15-स्तरीय प्राथमिकता पदानुक्रम और साहचर्य नियम)

[EN] C organizes its operators into 15 precedence levels, from tier 1 postfix operators (`() [] -> . x++ x--`) at the top down to tier 15 comma operator (`,`) at the bottom. Higher-precedence operators bind tighter than lower-precedence ones (e.g., `* / %` before `+ -`). When two operators in the same subexpression share EQUAL precedence, Associativity breaks the tie: most binary operators associate Left-to-Right (`a - b + c` becomes `(a - b) + c`), whereas only three categories associate Right-to-Left—Unary prefix operators (tier 2), Conditional `? :` (tier 13), and Assignment operators `= +=` (tier 14).

[HI] C अपने ऑपरेटर्स को 15 प्राथमिकता स्तरों में बांटता है—सबसे ऊपर स्तर 1 पर पोस्टफिक्स ऑपरेटर्स (`() [] -> . x++ x--`) और सबसे नीचे स्तर 15 पर कॉमा ऑपरेटर (`,`) आता है। समान प्राथमिकता वाले दो ऑपरेटर्स आने पर साहचर्य (Associativity) नियम निर्णय लेता है: अधिकांश बाइनरी ऑपरेटर्स बाएँ से दाएँ जुड़ते हैं, जबकि केवल तीन वर्ग—यूनरी प्रीफिक्स (स्तर 2), टर्नरी `? :` (स्तर 13) और असाइनमेंट `=` (स्तर 14)—दाएँ से बाएँ जुड़ते हैं।

2. Precedence vs Order of Evaluation Myth (प्राथमिकता बनाम मूल्यांकन क्रम (Order of Evaluation) का भ्रम)

[EN] One of the most misunderstood rules in C is confusing Precedence with Order of Evaluation. Precedence dictates WHICH operands group with WHICH operator (the parse tree), NOT which function or subexpression executes first at runtime! In `x = f() + g() * h()`, `*` has higher precedence than `+`, guaranteeing `g() * h()` multiplies before `+` adds—yet C leaves the calling order of `f()`, `g()`, and `h()` completely unspecified (`f()` might run first, or `h()` might run first!). Only four operators force left-to-right runtime evaluation order with sequence points: `&&`, `||`, `? :` (after condition), and `,`.

[HI] C में सबसे बड़ा भ्रम यह है कि लोग उच्च प्राथमिकता का अर्थ 'पहले रन होना' समझ लेते हैं। वास्तव में प्राथमिकता केवल यह तय करती है कि कौन सा ऑपरेंड किस ऑपरेटर से जुड़ेगा (Parse Tree), न कि रनटाइम पर कौन सा फंक्शन पहले चलेगा! जैसे `f() + g() * h()` में `*` का गुणा `+` से पहले होगा, परंतु `f()`, `g()` और `h()` में से कौन पहले कॉल होगा, यह अनिर्दिष्ट (Unspecified) है। केवल चार ऑपरेटर्स (`&&`, `||`, `? :`, `,`) ही बाएँ से दाएँ निश्चित मूल्यांकन क्रम देते हैं।

3. Notorious C Precedence Pitfalls in Competitive Exams (परीक्षाओं में पूछे जाने वाले प्रमुख प्राथमिकता ट्रैप)

[EN] Several classic C bugs stem directly from counter-intuitive precedence tiers: (1) Equality (`==`, `!=`, tier 7) binds TIGHTER than bitwise `&`, `^`, `|` (tiers 8-10), so `x & 1 == 0` parses wrongly as `x & (1 == 0)`; (2) Arithmetic `+ -` (tier 4) binds TIGHTER than bit-shifts `<< >>` (tier 5), so `1 << 2 + 1` means `1 << 3 = 8`; (3) Postfix `++` (tier 1) beats prefix `*` (tier 2), so `*p++` is `*(p++)`, while `*++p` (both tier 2, Right-to-Left) is `*(++p)`; (4) Logical `&&` (tier 11) beats `||` (tier 12).

[HI] C में कई प्रसिद्ध बग्स प्राथमिकता के कारण उत्पन्न होते हैं: (1) `==` व `!=` (स्तर 7) की प्राथमिकता बिटवाइज़ `&`, `^`, `|` (स्तर 8-10) से अधिक है, अतः `x & 1 == 0` गलत रूप से `x & (1 == 0)` बन जाता है; (2) `+` और `-` की प्राथमिकता `<<` व `>>` से अधिक है, इसलिए `1 << 2 + 1` का मान `1 << 3 = 8` होता है; (3) पोस्टफिक्स `++` यूनरी `*` से बड़ा है (`*p++ == *(p++)`); (4) लॉजिकल `&&` की प्राथमिकता `||` से अधिक है।

4. Comma Operator (,) & Parentheses Best Practices (कॉमा ऑपरेटर (,) व कोष्ठक उपयोग के नियम)

[EN] The comma operator (`,`) sits at the very bottom of the precedence table (tier 15) and associates Left-to-Right. It evaluates its left operand, enforces a sequence point, discards that value, evaluates its right operand, and returns the rightmost operand's value and type. Note the sharp difference between `x = (1, 2, 3);` (assigns `3` to `x`) and `x = 1, 2, 3;` (because `=` beats `,`, it parses as `(x = 1), 2, 3`, assigning `1` to `x`!). Also remember that commas separating function arguments or variable declarations (`int a = 1, b = 2;`) are punctuation separators, NOT comma operators.

[HI] कॉमा ऑपरेटर (`,`) प्राथमिकता तालिका में सबसे निचले स्तर 15 पर होता है और बाएँ से दाएँ चलता है। यह पहले बायाँ भाग चलाकर सीक्वेंस पॉइंट बनाता है, उसका मान त्यागता है और सबसे दाएँ एक्सप्रेशन का मान लौटाता है। `x = (1, 2, 3);` में `x` को `3` मिलता है, परंतु बिना कोष्ठक `x = 1, 2, 3;` में `=` की प्राथमिकता अधिक होने से `(x = 1), 2, 3` होकर `x` में `1` स्टोर होता है! फंक्शन आर्ग्युमेंट्स के बीच का कॉमा ऑपरेटर नहीं बल्कि सेपरेटर होता है।

Syntax, Formula & Technical Example (Operator Precedence and Associativity Rules in C)

#include <stdio.h>
int main() {
    int a = 10, b = 2, r1 = a + b * 3;
    int r2 = 10 & 2 == 2; // == binds tighter than &
    int r3 = (a = 3, a + 7);
    printf("r1=%d r2=%d r3=%d sh=%d\n", r1, r2, r3, 1 << 1 + 2);
    return 0;
}
// Output: r1=16 r2=0 r3=10 sh=8

Operator Precedence and Associativity Rules in C Quick Reference & Comparison Table

Precedence TierOperators & AssociativityExam Trap / Note
Postfix Tier (Highest)() [] -> . x++ x-- (Left-to-Right)Binds tighter than prefix unary * or ++
Unary Prefix Tier++x --x ! ~ (type) sizeof * & (Right-to-Left)Groups *++p cleanly as *(++p)
Ternary Conditional Tier? : (Right-to-Left)Higher than assignment =, lower than ||
Comma Operator (Lowest), (Left-to-Right)Evaluates LHS first then returns rightmost value

Operator Precedence and Associativity Rules in C Top 20 Bilingual MCQs with Answers

Q1. Which operator category holds the HIGHEST precedence in standard C?

(मानक C में किस ऑपरेटर श्रेणी की प्राथमिकता सबसे अधिक होती है?)

(A) Assignment operators: =, +=

(B) Postfix operators: (), [], ->, ., postfix ++ and --

(C) Multiplicative operators: *, /, %

(D) Unary prefix operators: ++x, !, ~, sizeof

Correct Answer: (B)

[EN] Postfix operators sit at tier 1 at the very top of C's 15-level precedence table, above unary prefix operators (tier 2).

[HI] पोस्टफिक्स ऑपरेटर्स `(), [], ->, ., x++, x--` C की प्राथमिकता तालिका में सबसे ऊपर (स्तर 1) पर होते हैं।

Q2. Which operator has the LOWEST precedence in C?

(C में किस ऑपरेटर की प्राथमिकता सबसे कम होती है?)

(A) Logical OR (||)

(B) Conditional (? :)

(C) Assignment operator (=)

(D) Comma operator (,)

Correct Answer: (D)

[EN] The comma operator sits at tier 15 (lowest), even below assignment operators at tier 14.

[HI] कॉमा ऑपरेटर (,) स्तर 15 पर सबसे कम प्राथमिकता रखता है—यह असाइनमेंट (=) से भी नीचे होता है।

Q3. Which three operator groups in C share Right-to-Left associativity?

(C में किन तीन ऑपरेटर समूहों का साहचर्य (Associativity) दाएँ से बाएँ (Right-to-Left) होता है?)

(A) Unary prefix operators, Ternary (? :), and Assignment (=, +=, ...)

(B) Postfix, Bitwise, and Comma operators

(C) Relational, Equality, and Logical &&/||

(D) Multiplicative, Additive, and Shift operators

Correct Answer: (A)

[EN] Only unary prefix (tier 2), conditional ? : (tier 13), and assignment operators (tier 14) associate right-to-left in C.

[HI] C में केवल यूनरी प्रीफिक्स, टर्नरी (`? :`) और असाइनमेंट ऑपरेटर्स ही दाएँ से बाएँ जुड़ते हैं; बाकी सभी बाएँ से दाएँ जुड़ते हैं।

Q4. What are the values stored in `x` and `y` by `int x, y; x = 1, 2, 3; y = (1, 2, 3);`?

(`int x, y; x = 1, 2, 3; y = (1, 2, 3);` से `x` और `y` में क्रमशः क्या मान स्टोर होंगे?)

(A) x = 3 and y = 1

(B) x = 3 and y = 3

(C) x = 1 and y = 3

(D) x = 1 and y = 1

Correct Answer: (C)

[EN] = has higher precedence than `,`, so `x = 1, 2, 3` parses as `(x = 1), 2, 3` setting x = 1; parentheses in `(1, 2, 3)` return 3 to y.

[HI] `=` की प्राथमिकता `,` से अधिक है अतः `x = 1, 2, 3` को `(x = 1), 2, 3` मानकर `x = 1` मिलता है, जबकि `(1, 2, 3)` अंतिम मान `3` लौटाकर `y = 3` करता है।

Q5. What does `printf("%d", 10 + 20 / 5 * 2 - 3);` output in C?

(C में `printf("%d", 10 + 20 / 5 * 2 - 3);` का आउटपुट क्या होगा?)

(A) 9

(B) 19

(C) 11

(D) 15

Correct Answer: (D)

[EN] Multiplicative / and * execute left-to-right: 20 / 5 = 4, 4 * 2 = 8; then additive + and - run left-to-right: 10 + 8 - 3 = 15.

[HI] पहले / और * बाएँ से दाएँ चलते हैं: 20/5 = 4, 4*2 = 8; फिर 10 + 8 - 3 = 15 प्रिंट होता है।

Q6. What is the output of `printf("%d", 1 << 2 + 1);` in C?

(C में `printf("%d", 1 << 2 + 1);` क्या प्रिंट करेगा?)

(A) 3

(B) 4

(C) 5

(D) 8

Correct Answer: (D)

[EN] Additive `+` (tier 4) has higher precedence than bitwise shift `<<` (tier 5), so it parses as `1 << (2 + 1)` = `1 << 3` = 8.

[HI] `+` की प्राथमिकता `<<` से अधिक होने से यह `1 << (2 + 1)` अर्थात `1 << 3` = 8 हल होता है।

Q7. What does `printf("%d", 5 & 3 == 3);` print in C?

(C में `printf("%d", 5 & 3 == 3);` क्या प्रिंट करेगा?)

(A) 1

(B) 5

(C) 3

(D) 0

Correct Answer: (A)

[EN] == (tier 7) has higher precedence than & (tier 8): 3 == 3 is 1, and 5 & 1 (0101 & 0001) equals 1.

[HI] `==` की प्राथमिकता `&` से अधिक होती है: पहले 3 == 3 से 1 मिलता है, फिर 5 & 1 = 1 प्रिंट होता है।

Q8. What is printed by `printf("%d", !0 == 1);` in C?

(C में `printf("%d", !0 == 1);` का आउटपुट क्या होगा?)

(A) 1

(B) 0

(C) -1

(D) Compile error

Correct Answer: (A)

[EN] Unary `!` (tier 2) binds tighter than `==` (tier 7): `(!0) == 1` -> `1 == 1` -> 1.

[HI] यूनरी `!` की प्राथमिकता `==` से बहुत अधिक होती है, अतः `(!0) == 1` अर्थात `1 == 1` होकर 1 प्रिंट होता है।

Q9. In `int res = f1() + f2() * f3();`, what does operator precedence guarantee at runtime?

(`int res = f1() + f2() * f3();` एक्सप्रेशन में ऑपरेटर प्राथमिकता रनटाइम पर क्या सुनिश्चित करती है?)

(A) That f1() executes first, then f2(), then f3()

(B) That f2() and f3() are guaranteed to execute before f1()

(C) Only that f2()'s result is multiplied by f3()'s result before adding f1(); call order of f1, f2, f3 is unspecified

(D) That f3() executes last always

Correct Answer: (C)

[EN] Precedence builds the AST grouping `f1() + (f2() * f3())`, while the runtime order in which the three functions get called remains unspecified in C.

[HI] प्राथमिकता केवल यह तय करती है कि पहले `f2()*f3()` गुणा होकर `f1()` में जुड़ेगा; परंतु तीनों में से कौन सा फंक्शन पहले कॉल होगा यह C में अनिर्दिष्ट है।

Q10. How does C parse `*++p` when `p` is an `int *` pointing into an array?

(जब `p` एक `int *` पॉइंटर हो, तो C कंपाइलर `*++p` को कैसे ग्रुप करता है?)

(A) *(++p) because unary * and prefix ++ share tier 2 and associate right-to-left

(B) (*p)++ because * has higher precedence than ++

(C) Syntax error without parentheses

(D) *(p++) because postfix always takes priority

Correct Answer: (A)

[EN] Unary dereference `*` and prefix `++` sit on the exact same tier (2) with Right-to-Left associativity, so `++` binds to `p` first.

[HI] यूनरी `*` और प्रीफिक्स `++` दोनों स्तर 2 पर हैं और दाएँ से बाएँ जुड़ते हैं, अतः पहले `++p` और फिर `*` लागू होकर `*(++p)` बनता है।

Q11. What is the output of `int a = 5, b = 10; int r = (a > 0, b += 5, a + b); printf("%d", r);`?

(`int a = 5, b = 10; int r = (a > 0, b += 5, a + b); printf("%d", r);` का आउटपुट क्या होगा?)

(A) 1

(B) 15

(C) 10

(D) 20

Correct Answer: (D)

[EN] The comma operator evaluates strictly left-to-right: a > 0 (discarded), b becomes 15 (discarded), and a + b = 5 + 15 = 20 is returned.

[HI] कॉमा ऑपरेटर बाएँ से दाएँ चलता है: `a > 0` के बाद `b += 5` से b=15 होता है और अंत में `a + b = 20` लौटता है।

Q12. In `int a = 1, b = 2; int x = a && b || 0;`, which operator groups `a` and `b` together?

(`int a = 1, b = 2; int x = a && b || 0;` में किस ऑपरेटर की प्राथमिकता अधिक होने से `a` और `b` पहले जुड़ते हैं?)

(A) = binds before &&

(B) && has higher precedence than ||

(C) Both share equal precedence right-to-left

(D) || has higher precedence than &&

Correct Answer: (B)

[EN] Logical AND (&&, tier 11) binds tighter than logical OR (||, tier 12).

[HI] लॉजिकल `&&` (स्तर 11) की प्राथमिकता लॉजिकल `||` (स्तर 12) से अधिक होती है।

Q13. What does `printf("%d", 10 - 4 - 2);` print and why?

(`printf("%d", 10 - 4 - 2);` क्या प्रिंट करता है और क्यों?)

(A) 4, because right-to-left gives 4

(B) 4, because binary subtraction associates left-to-right as (10 - 4) - 2

(C) 0

(D) 8, because subtraction associates right-to-left as 10 - (4 - 2)

Correct Answer: (B)

[EN] Additive operators (+ and -) associate left-to-right, computing (10 - 4) - 2 = 6 - 2 = 4.

[HI] घटाव (-) का साहचर्य बाएँ से दाएँ होता है, अतः `(10 - 4) - 2` = 4 प्रिंट होता है।

Q14. Given `int x = 10; int *p = &x;`, why is `sizeof *p` valid without parentheses and what does it compute?

(`int x = 10; int *p = &x;` होने पर `sizeof *p` बिना कोष्ठक वैध क्यों है और यह क्या मापता है?)

(A) It computes sizeof(int*) (size of pointer p)

(B) It multiplies sizeof by p

(C) It causes a syntax error without parentheses

(D) Because *p is a unary expression; it computes sizeof(int) (size of the pointed value)

Correct Answer: (D)

[EN] `sizeof` accepts any unary-expression without parentheses, and `*p` is an `int` expression, yielding `sizeof(int)`.

[HI] `sizeof` बिना कोष्ठक के यूनरी एक्सप्रेशन ले सकता है और `*p` का टाइप `int` होने से यह `sizeof(int)` का आकार देता है।

Q15. Which statement is NOT true about operator precedence and associativity in C?

(C में ऑपरेटर प्राथमिकता और साहचर्य के बारे में इनमें से कौन सा कथन असत्य है?)

(A) Associativity resolves grouping only when adjacent operators share equal precedence

(B) Assignment operators associate right-to-left

(C) Higher precedence guarantees that the higher-precedence subexpression executes first at runtime

(D) Postfix ++ binds tighter than unary prefix *

Correct Answer: (C)

[EN] Precedence governs syntactic grouping (parse tree), NOT runtime execution order of side-effecting subexpressions.

[HI] ऑपरेटर प्राथमिकता केवल सिंटैक्स ग्रुपिंग तय करती है, यह रनटाइम निष्पादन क्रम (Order of Evaluation) की गारंटी नहीं देती।

Q16. What is the value of `int a = 3; printf("%d", a << 1 * 2 + 1);`?

(`int a = 3; printf("%d", a << 1 * 2 + 1);` का मान क्या होगा?)

(A) 12

(B) 13

(C) 7

(D) 24

Correct Answer: (D)

[EN] Precedence order * > + > <<: 1 * 2 = 2, 2 + 1 = 3, then 3 << 3 = 3 * 8 = 24.

[HI] प्राथमिकता (* > + > <<) से पहले `1 * 2 = 2`, फिर `2 + 1 = 3`, और अंत में `3 << 3 = 24` प्राप्त होता है।

Q17. Within Operator Precedence and Associativity Rules in C, which precedence tier directly maps to: "() [] -> . x++ x-- (Left-to-Right)"?

(Operator Precedence and Associativity Rules in C में कौन सा Precedence Tier सीधे "() [] -> . x++ x-- (Left-to-Right)" से संबंधित है?)

(A) Comma Operator (Lowest)

(B) Ternary Conditional Tier

(C) Unary Prefix Tier

(D) Postfix Tier (Highest)

Correct Answer: (D)

[EN] In Operator Precedence and Associativity Rules in C, 'Postfix Tier (Highest)' handles '() [] -> . x++ x-- (Left-to-Right)' (Binds tighter than prefix unary * or ++).

[HI] Operator Precedence and Associativity Rules in C में 'Postfix Tier (Highest)' का कार्य/विवरण '() [] -> . x++ x-- (Left-to-Right)' (Binds tighter than prefix unary * or ++) है।

Q18. Which option accurately describes 'Unary Prefix Tier' in Operator Precedence and Associativity Rules in C?

(Operator Precedence and Associativity Rules in C के संदर्भ में 'Unary Prefix Tier' की सही विशेषता कौन सी है?)

(A) ? : (Right-to-Left)

(B) , (Left-to-Right)

(C) ++x --x ! ~ (type) sizeof * & (Right-to-Left)

(D) () [] -> . x++ x-- (Left-to-Right)

Correct Answer: (C)

[EN] 'Unary Prefix Tier' provides '++x --x ! ~ (type) sizeof * & (Right-to-Left)' — key point: Groups *++p cleanly as *(++p).

[HI] 'Unary Prefix Tier' का मुख्य विवरण '++x --x ! ~ (type) sizeof * & (Right-to-Left)' है (Groups *++p cleanly as *(++p))।

Q19. In Operator Precedence and Associativity Rules in C, identify the correct parameter for the specification: "? : (Right-to-Left)".

(Operator Precedence and Associativity Rules in C में "? : (Right-to-Left)" विवरण किस विकल्प के लिए सही है?)

(A) Unary Prefix Tier

(B) Comma Operator (Lowest)

(C) Ternary Conditional Tier

(D) Postfix Tier (Highest)

Correct Answer: (C)

[EN] 'Ternary Conditional Tier' corresponds to '? : (Right-to-Left)' (Higher than assignment =, lower than ||).

[HI] 'Ternary Conditional Tier' का संबंध '? : (Right-to-Left)' (Higher than assignment =, lower than ||) से है।

Q20. Considering Operator Precedence and Associativity Rules in C, what key behavior defines 'Comma Operator (Lowest)'?

(Operator Precedence and Associativity Rules in C में 'Comma Operator (Lowest)' की मुख्य भूमिका या मान क्या है?)

(A) ? : (Right-to-Left)

(B) () [] -> . x++ x-- (Left-to-Right)

(C) ++x --x ! ~ (type) sizeof * & (Right-to-Left)

(D) , (Left-to-Right)

Correct Answer: (D)

[EN] 'Comma Operator (Lowest)' represents ', (Left-to-Right)' (Evaluates LHS first then returns rightmost value).

[HI] 'Comma Operator (Lowest)' का अर्थ/मान ', (Left-to-Right)' है (Evaluates LHS first then returns rightmost value)।

Operator Precedence and Associativity Rules in C — Exam FAQs

Q1: What is the difference between precedence, associativity, and order of evaluation?

ऑपरेटर प्राथमिकता (Precedence), साहचर्य (Associativity) और मूल्यांकन क्रम (Order of Evaluation) में क्या अंतर है?

[EN] Precedence decides which operators bind tighter when different operators meet (`a + b * c` -> `a + (b * c)`). Associativity breaks ties among equal-precedence operators (`a - b - c` -> `(a - b) - c`). Order of evaluation dictates which operand runs first at runtime—which C leaves unspecified for most operators except `&&`, `||`, `? :`, and `,`.

[HI] प्राथमिकता अलग-अलग ऑपरेटर्स का जुड़ाव तय करती है, साहचर्य समान प्राथमिकता वाले ऑपरेटर्स के बीच बाएँ/दाएँ का निर्णय लेता है, जबकि मूल्यांकन क्रम यह बताता है कि रनटाइम पर कौन सा हिस्सा पहले चलेगा (जो अधिकतर ऑपरेटर्स के लिए C में अनिर्दिष्ट होता है)।

Q2: Why did `x = 1, 2, 3;` set `x` to `1` instead of `3`?

`x = 1, 2, 3;` लिखने पर `x` का मान `3` के बजाय `1` क्यों आता है?

[EN] Because `=` has higher precedence than the comma operator `,`, C parses `x = 1, 2, 3` as `(x = 1), 2, 3`. Put parentheses `(1, 2, 3)` to make the comma expression execute before assignment.

[HI] `=` की प्राथमिकता कॉमा `,` से अधिक होती है, अतः कंपाइलर इसे `(x = 1), 2, 3` पढ़ता है। पूरे कॉमा व्यंजक का अंतिम मान `3` लेने के लिए `x = (1, 2, 3);` लिखना होगा।

Q3: Why do bitwise tests like `if (status & READY == READY)` often fail silently?

`if (status & READY == READY)` बिना कोष्ठक के चुपचाप गलत परिणाम क्यों देता है?

[EN] Equality (`==`) sits above bitwise AND (`&`) in C's precedence hierarchy, so it parses as `status & (READY == READY)` = `status & 1`. Always enclose bitwise operations in parentheses: `(status & READY) == READY`.

[HI] `==` की प्राथमिकता `&` से अधिक होने से यह `status & (READY == READY)` यानी `status & 1` बन जाता है। सही जांच के लिए हमेशा `(status & READY) == READY` लिखें।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)