Assignment Operators and Compound Assignment (+=, -=) Notes in Hindi & English : असाइनमेंट ऑपरेटर्स और कंपाउंड असाइनमेंट का उपयोग और 20 महत्वपूर्ण MCQ प्रश्न (Rajasthan IT Exam)

Post a Comment

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

Overview: Assignment (=) and compound assignment operators (+=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=) store values into modifiable lvalues using right-to-left associativity. Compound operators evaluate the left-hand expression only once and implicitly parenthesize the right-hand expression.

अध्याय सार: C में असाइनमेंट (=) और कंपाउंड असाइनमेंट ऑपरेटर्स (+=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=) दाएँ से बाएँ साहचर्य के साथ मॉडिफाएबल एल-वैल्यू (Lvalue) में मान स्टोर करते हैं। कंपाउंड ऑपरेटर्स बाएँ हिस्से का मूल्यांकन केवल एक बार करते हैं और दाएँ एक्सप्रेशन को स्वतः कोष्ठक में मानते हैं।

Assignment Operators and Compound Assignment (+=, -=): Detailed Notes & Architecture (असाइनमेंट ऑपरेटर्स और कंपाउंड असाइनमेंट का उपयोग)

1. Simple Assignment (=), Modifiable Lvalues & Right-to-Left Chaining (साधारण असाइनमेंट (=), एल-वैल्यू नियम व चेनिंग)

[EN] The simple assignment operator `=` evaluates its right-hand side (rvalue), converts it to the type of the left-hand side (modifiable lvalue), stores it in that object's memory, and yields the stored value as the expression result. A modifiable lvalue denotes an assignable memory object (not an array name, const variable, literal, or arithmetic expression like `a + b`). All assignment operators associate right-to-left and have the second-lowest precedence (just above the comma operator). Thus `a = b = c = 10` parses as `a = (b = (c = 10))`.

[HI] साधारण असाइनमेंट ऑपरेटर `=` दाएँ भाग का मान निकालकर बाएँ हिस्से (Modifiable Lvalue) की मेमोरी में संग्रहीत करता है और वही मान एक्सप्रेशन के परिणाम के रूप में लौटाता है। एक मॉडिफाएबल Lvalue एक परिवर्तनीय मेमोरी स्थान होता है—अतः एरे का नाम, const वेरिएबल, या `(a + b)` एल-वैल्यू नहीं बन सकते। सभी असाइनमेंट ऑपरेटर्स दाएँ से बाएँ (Right-to-Left) जुड़ते हैं, जिससे `a = b = c = 10` को `a = (b = (c = 10))` के रूप में हल किया जाता है।

2. Compound Assignment Operators & Implicit RHS Parenthesizing (कंपाउंड असाइनमेंट और दाएँ एक्सप्रेशन के अंतर्निहित कोष्ठक)

[EN] C supports ten compound assignment operators: five arithmetic (`+=`, `-=`, `*=`, `/=`, `%=`) and five bitwise (`<<=`, `>>=`, `&=`, `^=`, `|=`). A critical rule often tested in exams is that `a op= expr` treats the ENTIRE right-hand side as parenthesized: `a = a op (expr)`. For instance, if `a = 5`, `a *= 2 + 3` computes `5 * (2 + 3) = 25`, NOT `5 * 2 + 3 = 13`! Similarly, `x /= a + b` means `x = x / (a + b)`, preventing subtle precedence errors.

[HI] C में कुल 10 कंपाउंड असाइनमेंट ऑपरेटर्स होते हैं: 5 अंकगणितीय (`+=`, `-=`, `*=`, `/=`, `%=`) तथा 5 बिटवाइज़ (`<<=`, `>>=`, `&=`, `^=`, `|=`)। परीक्षा का एक बहुत महत्वपूर्ण नियम यह है कि `a op= expr` में पूरा दायाँ व्यंजक कोष्ठक में माना जाता है, अर्थात `a = a op (expr)`। उदाहरण के लिए `a = 5` होने पर `a *= 2 + 3` का मान `5 * (2 + 3) = 25` होता है, न कि `13`।

3. Single-Evaluation Guarantee in Compound Assignment (कंपाउंड असाइनमेंट में सिंगल-इवैल्यूएशन गारंटी)

[EN] Unlike manually writing `lhs = lhs + rhs`, ISO C guarantees that `lhs += rhs` evaluates the lvalue expression `lhs` ONLY ONCE. Consider `arr[i++] += 10;`: with compound assignment, `i++` executes once, incrementing the element at the original index `i` by 10 and advancing `i` by 1. Had you written `arr[i++] = arr[i++] + 10;`, it would modify `i` twice without an intervening sequence point—invoking Undefined Behavior! Single evaluation also avoids calling side-effecting index functions twice.

[HI] `lhs = lhs + rhs` लिखने के विपरीत, ISO C में `lhs += rhs` लिखने पर बायाँ एक्सप्रेशन `lhs` केवल एक ही बार मूल्यांकित होता है। जैसे `arr[i++] += 10` में `i++` केवल एक बार चलता है और सुरक्षित रूप से काम पूरा होता है, जबकि `arr[i++] = arr[i++] + 10` में बिना सीक्वेंस पॉइंट के `i` दो बार बदलने से Undefined Behavior उत्पन्न हो जाता है।

4. Implicit Narrowing Conversions & Historical =+ Note (असाइनमेंट के दौरान टाइप रूपांतरण व =+ की सावधानी)

[EN] Assigning a wider value into a narrower type applies implicit conversion: `int x = 9.87;` silently truncates the fraction to `9` (discarding 0.87), and `unsigned char c = 300;` reduces modulo 256 to `44`. Compound operators work identically (`int x = 5; x += 2.9;` converts `5 + 2.9 = 7.9` back to `7`). Watch out for spacing typos like `a =+ 5` or `a =- 5`: in modern C, `=+` is NOT a compound operator—it parses as assignment of unary plus `a = (+5)` or unary minus `a = (-5)`!

[HI] असाइनमेंट करते समय दाएँ मान को बाएँ वेरिएबल के डेटा टाइप में स्वतः रूपांतरित किया जाता है: जैसे `int x = 9.87;` दशमलव हटाकर `9` स्टोर करता है और `unsigned char c = 300;` मॉड्यूलो 256 से `44` रखता है। इसके अलावा `a =+ 5` या `a =- 5` लिखने की सावधानी रखें: आधुनिक C में ये कंपाउंड ऑपरेटर नहीं हैं, बल्कि `a = (+5)` और `a = (-5)` (यूनरी चिह्न के साथ साधारण असाइनमेंट) के रूप में पढ़े जाते हैं।

Syntax, Formula & Technical Example (Assignment Operators and Compound Assignment (+=, -=))

#include <stdio.h>
int main() {
    int a, b = 5, i = 0, arr[2] = {10, 20};
    a = b *= 2 + 3; // b = 5 * (2 + 3) = 25, a = 25
    arr[i++] += 5;  // evaluates i++ once: arr[0]=15, i=1
    printf("a=%d b=%d arr0=%d i=%d\n", a, b, arr[0], i);
    return 0;
}
// Output: a=25 b=25 arr0=15 i=1

Assignment Operators and Compound Assignment (+=, -=) Quick Reference & Comparison Table

Assignment FormEvaluation RuleKey Behavioral Detail
Simple Assignment (=)Assigns RHS to modifiable LHS lvalue and returns valueRejects non-lvalues like literals or const vars
Chained Assignment (a=b=c)Associates right-to-left assigning c into b then aPermits multi-variable initialization in one step
Compound Op (a *= b + c)Implicitly groups RHS as a = a * (b + c)Prevents precedence bugs on complex RHS
Indexed Compound (arr[i++]+=5)Evaluates LHS index expression strictly onceAvoids double-increment Undefined Behavior

Assignment Operators and Compound Assignment (+=, -=) Top 20 Bilingual MCQs with Answers

Q1. What is the associativity of assignment (`=`) and compound assignment (`+=`, `*=`) operators in C?

(C में असाइनमेंट (`=`) व कंपाउंड असाइनमेंट (`+=`, `*=`) ऑपरेटर्स का साहचर्य (Associativity) क्या होता है?)

(A) Compiler-dependent

(B) Right-to-Left

(C) Left-to-Right

(D) Non-associative

Correct Answer: (B)

[EN] All assignment operators in C associate right-to-left, so a = b = 10 parses as a = (b = 10).

[HI] C में सभी असाइनमेंट ऑपरेटर्स का साहचर्य दाएँ से बाएँ (Right-to-Left) होता है, जिससे a = b = 10 पहले b = 10 करता है।

Q2. Given `int x = 4; x *= 3 + 2;`, what is the final value of `x`?

(`int x = 4; x *= 3 + 2;` निष्पादित होने के बाद `x` का अंतिम मान क्या होगा?)

(A) 20

(B) 14

(C) 10

(D) 24

Correct Answer: (A)

[EN] Compound assignment wraps the entire RHS in parentheses: x = x * (3 + 2) = 4 * 5 = 20, NOT 4 * 3 + 2 = 14.

[HI] कंपाउंड असाइनमेंट पूरे दाएँ एक्सप्रेशन को कोष्ठक में मानता है: x = 4 * (3 + 2) = 20, न कि 14।

Q3. What is the output of `int a = 10; a =- 3; printf("%d", a);` in modern C?

(आधुनिक C में `int a = 10; a =- 3; printf("%d", a);` का आउटपुट क्या होगा?)

(A) 13

(B) Compile error

(C) 7

(D) -3

Correct Answer: (D)

[EN] There is no `=-` operator in C; the lexer tokenizes `a =- 3` as `a = (-3)`, assigning -3 to a.

[HI] आधुनिक C में `=-` जैसा कोई कंपाउंड ऑपरेटर नहीं होता; कंपाइलर इसे `a = (-3)` पढ़कर a में -3 असाइन कर देता है।

Q4. What happens when compiling `int arr[3] = {1, 2, 3}; int b[3] = {4, 5, 6}; arr = b;`?

(`int arr[3] = {1, 2, 3}; int b[3] = {4, 5, 6}; arr = b;` को कंपाइल करने पर क्या होगा?)

(A) Copies all 3 integers cleanly

(B) Undefined Behavior at runtime

(C) Compile error: array type is not a modifiable lvalue

(D) Copies only the first element

Correct Answer: (C)

[EN] An array name in C is a non-modifiable lvalue and cannot appear on the LHS of `=`; memcpy() is required to copy arrays.

[HI] C में एरे का नाम मॉडिफाएबल Lvalue नहीं होता, अतः एरे पर सीधा असाइनमेंट `=` कंपाइल त्रुटि देता है।

Q5. Why is `arr[i++] += 2;` well-defined in C while `arr[i++] = arr[i++] + 2;` is Undefined Behavior?

(C में `arr[i++] += 2;` वैध क्यों है जबकि `arr[i++] = arr[i++] + 2;` अनडिफाइंड बिहेवियर क्यों बनाता है?)

(A) Compound assignment evaluates the LHS lvalue (i++) strictly once

(B) Because += creates a sequence point before +=

(C) Because + has higher precedence than +=

(D) Because = cannot index arrays

Correct Answer: (A)

[EN] ISO C guarantees single evaluation of the LHS in compound assignment, whereas repeating i++ on both sides mutates i twice unsequenced.

[HI] कंपाउंड असाइनमेंट में LHS (i++) केवल एक बार मूल्यांकित होता है, जबकि दोनों ओर i++ लिखने से बिना सीक्वेंस पॉइंट के दो बार बदलाव होकर UB हो जाता है।

Q6. What is the output of `int x = 5; x += 3.9; printf("%d", x);`?

(`int x = 5; x += 3.9; printf("%d", x);` का आउटपुट क्या होगा?)

(A) 8.9

(B) Compile error

(C) 8

(D) 9

Correct Answer: (C)

[EN] 5 promotes to double 5.0 + 3.9 = 8.9, and storing back into int x truncates 8.9 toward zero to 8.

[HI] 5 + 3.9 = 8.9 होता है, जिसे वापस int वेरिएबल x में स्टोर करते समय दशमलव हटकर 8 हो जाता है।

Q7. Given `int a = 10, b = 20;`, what does `printf("%d", (a = 5) + (b = 7));` output?

(`int a = 10, b = 20;` होने पर `printf("%d", (a = 5) + (b = 7));` क्या प्रिंट करेगा?)

(A) 17

(B) 12

(C) 30

(D) Compile error

Correct Answer: (B)

[EN] Each parenthesized assignment returns the stored value: 5 + 7 = 12.

[HI] प्रत्येक असाइनमेंट एक्सप्रेशन स्टोर किया गया मान लौटाता है, इसलिए 5 + 7 = 12 प्रिंट होता है।

Q8. Which of the following is NOT a valid compound assignment operator in C?

(इनमें से कौन सा C में एक वैध कंपाउंड असाइनमेंट ऑपरेटर नहीं है?)

(A) ^=

(B) %=

(C) <<=

(D) &&=

Correct Answer: (D)

[EN] C has 10 compound operators (+=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=), and does NOT provide logical &&= or ||=.

[HI] C में 10 कंपाउंड असाइनमेंट ऑपरेटर्स होते हैं, लेकिन लॉजिकल `&&=` या `||=` ऑपरेटर C में उपलब्ध नहीं हैं।

Q9. What does `unsigned char c = 250; c += 10; printf("%u", c);` print?

(`unsigned char c = 250; c += 10; printf("%u", c);` क्या प्रिंट करेगा?)

(A) 0

(B) 255

(C) 260

(D) 4

Correct Answer: (D)

[EN] 250 + 10 = 260, and storing into 8-bit unsigned char wraps modulo 256: 260 % 256 = 4.

[HI] 250 + 10 = 260 को 8-बिट unsigned char में रखने पर मॉड्यूलो 256 के कारण 260 - 256 = 4 प्राप्त होता है।

Q10. What happens in `int x = 10; x <<= 2; x >>= 1; printf("%d", x);`?

(`int x = 10; x <<= 2; x >>= 1; printf("%d", x);` में क्या आउटपुट मिलेगा?)

(A) 20

(B) 40

(C) 10

(D) 5

Correct Answer: (A)

[EN] x <<= 2 multiplies 10 by 4 giving 40; x >>= 1 divides 40 by 2 giving 20.

[HI] x <<= 2 से 10 * 4 = 40 बनता है और x >>= 1 से 40 / 2 = 20 प्राप्त होता है।

Q11. What is the value of `int a = 2, b = 3; a += b += 4; printf("%d %d", a, b);`?

(`int a = 2, b = 3; a += b += 4; printf("%d %d", a, b);` का आउटपुट क्या होगा?)

(A) 6 7

(B) 7 7

(C) 9 7

(D) 9 4

Correct Answer: (C)

[EN] Right-to-left associativity runs b += 4 first (b becomes 7 and returns 7), then a += 7 sets a = 2 + 7 = 9.

[HI] दाएँ से बाएँ चलने पर पहले b += 4 से b = 7 बनता है, फिर a += 7 से a = 2 + 7 = 9 हो जाता है।

Q12. Which error occurs for `const int max = 100; max += 10;`?

(`const int max = 100; max += 10;` लिखने पर क्या त्रुटि उत्पन्न होगी?)

(A) Undefined Behavior

(B) Runtime segmentation fault

(C) max quietly updates to 110

(D) Assignment of read-only variable 'max' (compile error)

Correct Answer: (D)

[EN] A const-qualified object is an lvalue but NOT a modifiable lvalue, so modifying it via = or += fails at compile time.

[HI] const घोषित किया गया वेरिएबल परिवर्तनीय (Modifiable) एल-वैल्यू नहीं होता, अतः += लिखने पर कंपाइल त्रुटि आती है।

Q13. In the C operator precedence table, which single operator has LOWER precedence than assignment operators (`=`, `+=`)?

(C की ऑपरेटर प्राथमिकता तालिका में असाइनमेंट ऑपरेटर्स (`=`, `+=`) से भी कम प्राथमिकता किस एकमात्र ऑपरेटर की होती है?)

(A) Logical OR (||)

(B) Bitwise OR (|)

(C) Comma operator (,)

(D) Ternary operator (? :)

Correct Answer: (C)

[EN] Assignment operators sit at level 14, above only the comma operator (level 15).

[HI] असाइनमेंट ऑपरेटर्स 14वें स्तर पर होते हैं; इनसे कम प्राथमिकता केवल कॉमा ऑपरेटर (,) की होती है।

Q14. What is the output of `int x = 18; x %= 5; printf("%d", x);`?

(`int x = 18; x %= 5; printf("%d", x);` क्या प्रिंट करेगा?)

(A) 1

(B) 2

(C) 4

(D) 3

Correct Answer: (D)

[EN] x %= 5 computes x = 18 % 5 = 3.

[HI] x %= 5 का अर्थ x = 18 % 5 है, जिसका शेषफल 3 आता है।

Q15. Which statement is NOT true about assignment operators in C?

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

(A) Compound operators associate right-to-left

(B) Compound assignment evaluates the left operand only once

(C) Chained expression `a = b = c = 5;` works if a, b, c are already declared

(D) In `int a = b = c = 5;`, all three variables can be chained directly inside a declaration

Correct Answer: (D)

[EN] Wait carefully: `int a = b = c = 5;` fails to compile if b and c are not yet declared! Declarations use `,` (`int a = 5, b = 5, c = 5;`), not `=` chaining of undeclared names.

[HI] `int a = b = c = 5;` में यदि b और c पहले से घोषित न हों तो कंपाइल एरर आता है; डिक्लेरेशन के भीतर वेरिएबल्स को अलग-अलग कॉमा से इनिशियलाइज करना होता है।

Q16. What does `int m = 12; m &= 7; printf("%d", m);` print?

(`int m = 12; m &= 7; printf("%d", m);` का आउटपुट क्या होगा?)

(A) 15

(B) 12

(C) 7

(D) 4

Correct Answer: (D)

[EN] 12 is 1100 and 7 is 0111 in binary; 1100 & 0111 = 0100 = 4.

[HI] बाइनरी में 12 (1100) और 7 (0111) का बिटवाइज़ AND 0100 अर्थात 4 होता है।

Q17. Within Assignment Operators and Compound Assignment (+=, -=), which assignment form directly maps to: "Assigns RHS to modifiable LHS lvalue and returns value"?

(Assignment Operators and Compound Assignment (+=, -=) में कौन सा Assignment Form सीधे "Assigns RHS to modifiable LHS lvalue and returns value" से संबंधित है?)

(A) Compound Op (a *= b + c)

(B) Simple Assignment (=)

(C) Chained Assignment (a=b=c)

(D) Indexed Compound (arr[i++]+=5)

Correct Answer: (B)

[EN] In Assignment Operators and Compound Assignment (+=, -=), 'Simple Assignment (=)' handles 'Assigns RHS to modifiable LHS lvalue and returns value' (Rejects non-lvalues like literals or const vars).

[HI] Assignment Operators and Compound Assignment (+=, -=) में 'Simple Assignment (=)' का कार्य/विवरण 'Assigns RHS to modifiable LHS lvalue and returns value' (Rejects non-lvalues like literals or const vars) है।

Q18. Which option accurately describes 'Chained Assignment (a=b=c)' in Assignment Operators and Compound Assignment (+=, -=)?

(Assignment Operators and Compound Assignment (+=, -=) के संदर्भ में 'Chained Assignment (a=b=c)' की सही विशेषता कौन सी है?)

(A) Assigns RHS to modifiable LHS lvalue and returns value

(B) Implicitly groups RHS as a = a * (b + c)

(C) Associates right-to-left assigning c into b then a

(D) Evaluates LHS index expression strictly once

Correct Answer: (C)

[EN] 'Chained Assignment (a=b=c)' provides 'Associates right-to-left assigning c into b then a' — key point: Permits multi-variable initialization in one step.

[HI] 'Chained Assignment (a=b=c)' का मुख्य विवरण 'Associates right-to-left assigning c into b then a' है (Permits multi-variable initialization in one step)।

Q19. In Assignment Operators and Compound Assignment (+=, -=), identify the correct parameter for the specification: "Implicitly groups RHS as a = a * (b + c)".

(Assignment Operators and Compound Assignment (+=, -=) में "Implicitly groups RHS as a = a * (b + c)" विवरण किस विकल्प के लिए सही है?)

(A) Chained Assignment (a=b=c)

(B) Simple Assignment (=)

(C) Indexed Compound (arr[i++]+=5)

(D) Compound Op (a *= b + c)

Correct Answer: (D)

[EN] 'Compound Op (a *= b + c)' corresponds to 'Implicitly groups RHS as a = a * (b + c)' (Prevents precedence bugs on complex RHS).

[HI] 'Compound Op (a *= b + c)' का संबंध 'Implicitly groups RHS as a = a * (b + c)' (Prevents precedence bugs on complex RHS) से है।

Q20. Considering Assignment Operators and Compound Assignment (+=, -=), what key behavior defines 'Indexed Compound (arr[i++]+=5)'?

(Assignment Operators and Compound Assignment (+=, -=) में 'Indexed Compound (arr[i++]+=5)' की मुख्य भूमिका या मान क्या है?)

(A) Implicitly groups RHS as a = a * (b + c)

(B) Associates right-to-left assigning c into b then a

(C) Evaluates LHS index expression strictly once

(D) Assigns RHS to modifiable LHS lvalue and returns value

Correct Answer: (C)

[EN] 'Indexed Compound (arr[i++]+=5)' represents 'Evaluates LHS index expression strictly once' (Avoids double-increment Undefined Behavior).

[HI] 'Indexed Compound (arr[i++]+=5)' का अर्थ/मान 'Evaluates LHS index expression strictly once' है (Avoids double-increment Undefined Behavior)।

Assignment Operators and Compound Assignment (+=, -=) — Exam FAQs

Q1: Why is `int a = b = c = 0;` invalid in C when `b` and `c` are not declared earlier?

यदि `b` और `c` पहले से घोषित न हों, तो `int a = b = c = 0;` C में गलत क्यों माना जाता है?

[EN] In a declaration statement `int a = ...`, the type `int` applies only to `a` (unless variables are comma-separated like `int a, b, c;`). Using `b` and `c` inside the initializer treats them as already-existing variables, causing an undeclared identifier error.

[HI] डिक्लेरेशन `int a = ...` में `int` केवल पहले नाम `a` को घोषित करता है; जब तक `b` और `c` पहले घोषित न हों, उन्हें `a` के मान में उपयोग नहीं किया जा सकता। पहले `int a, b, c; a = b = c = 0;` लिखें।

Q2: Does `x += 1` run faster than `x = x + 1` in modern C compilers?

क्या आधुनिक C कंपाइलर में `x += 1` सचमुच `x = x + 1` से तेज चलता है?

[EN] For simple local variables, modern compilers generate identical machine code for both. However, for complex expressions like `arr[idx()].val += 1`, compound assignment is guaranteed to compute `idx()` only once, avoiding duplicate work and side-effect bugs.

[HI] साधारण वेरिएबल के लिए कंपाइलर दोनों का समान मशीन कोड बनाते हैं, परंतु `arr[idx()].val += 1` जैसे जटिल बाएँ हिस्से के लिए कंपाउंड असाइनमेंट `idx()` को केवल एक बार चलाकर तेज व सुरक्षित निष्पादन देता है।

Q3: Why does `x *= y + z` evaluate to `x * (y + z)` instead of `x * y + z`?

`x *= y + z` का मान `x * y + z` के बजाय `x * (y + z)` क्यों होता है?

[EN] Because compound assignment (`*=`) has lower precedence than `+`, the addition `y + z` is evaluated completely first as the right-hand operand of `*=`, effectively wrapping the RHS in parentheses.

[HI] चूंकि `*=` की प्राथमिकता `+` से कम होती है, इसलिए पूरा दायाँ भाग `y + z` पहले हल होता है और फिर `x` से गुणा होता है।

Related Posts

  • Loading related posts...

Post a Comment

Search Topic Notes & 20 MCQs (Hindi / English)