PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation) (लॉजिकल ऑपरेटर्स: AND, OR, NOT और शॉर्ट-सर्किट मूल्यांकन) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Logical operators in C (&&, ||, !) combine truth values where 0 represents false and any non-zero scalar represents true, always returning int 1 or 0. Binary logical operators && and || guarantee left-to-right short-circuit evaluation with a built-in sequence point.
अध्याय सार: C में लॉजिकल ऑपरेटर्स (&&, ||, !) बूलियन तर्क जोड़ते हैं जहाँ 0 असत्य तथा कोई भी गैर-शून्य (Non-zero) मान सत्य माना जाता है और परिणाम सदैव int 1 या 0 मिलता है। && तथा || बाएँ से दाएँ शॉर्ट-सर्किट मूल्यांकन व सीक्वेंस पॉइंट सुनिश्चित करते हैं।
Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation): Detailed Notes & Architecture (लॉजिकल ऑपरेटर्स: AND, OR, NOT और शॉर्ट-सर्किट मूल्यांकन)
1. Logical Operators, Scalar Truthiness & Exact Integer Outputs (लॉजिकल ऑपरेटर्स, सत्यता नियम व पूर्णांक आउटपुट)
[EN] C provides three logical operators: binary logical AND (&&), binary logical OR (||), and unary logical NOT (!). Any scalar operand—integer, floating-point, or pointer—is treated as false if and only if it equals 0 (or 0.0 or NULL), and as true for every non-zero value including negative integers like -7. Regardless of input magnitudes, the result of &&, ||, and ! is strictly int 1 (true) or int 0 (false). The double-negation idiom `!!x` is widely used in C to normalize any non-zero value directly into 1 while keeping 0 as 0.
[HI] C में तीन लॉजिकल ऑपरेटर्स होते हैं: बाइनरी लॉजिकल AND (&&), बाइनरी लॉजिकल OR (||) और यूनरी लॉजिकल NOT (!)। इनमें शून्य (0, 0.0 या NULL) को असत्य और किसी भी गैर-शून्य मान (यहाँ तक कि -7 जैसे ऋणात्मक मान को भी) सत्य माना जाता है। परिणाम हमेशा int 1 या 0 ही होता है। किसी भी मान को सख्ती से 1 या 0 में बदलने के लिए C में `!!x` तकनीक बहुतायत से प्रयोग की जाती है।
2. Short-Circuit Evaluation & Safety Guard Idioms (शॉर्ट-सर्किट मूल्यांकन व सुरक्षात्मक कोडिंग नियम)
[EN] Unlike bitwise & and |, logical operators && and || perform short-circuit (lazy) evaluation strictly from left to right. In `A && B`, if `A` evaluates to 0, the entire expression must be 0, so `B` is never evaluated at all. Similarly, in `A || B`, if `A` evaluates to non-zero, the entire expression is 1, so `B` is skipped. This behavior is essential for writing safe guard conditions in C, such as `if (ptr != NULL && *ptr == 'A')` and `if (i < n && arr[i] == key)`, preventing null-pointer dereferences or out-of-bounds memory reads.
[HI] बिटवाइज़ & और | के विपरीत, && और || बाएँ से दाएँ शॉर्ट-सर्किट मूल्यांकन करते हैं। `A && B` में यदि `A` का मान 0 हो जाए, तो परिणाम 0 तय हो जाने से `B` को रन ही नहीं किया जाता। इसी तरह `A || B` में `A` गैर-शून्य (सत्य) होने पर `B` को छोड़ दिया जाता है। इसी नियम के कारण `ptr != NULL && *ptr == 'A'` और `i < n && arr[i] == key` जैसे सुरक्षित गार्ड पैटर्न बिना सेगमेंटेशन फॉल्ट (Crash) के काम करते हैं।
3. Sequence Points & Side-Effect Pitfalls (सीक्वेंस पॉइंट और साइड-इफेक्ट के खतरे)
[EN] ISO C guarantees a sequence point immediately after evaluating the left operand of both && and ||. Therefore, every side effect on the left—such as `i++ < n && arr[i] > 0`—completes fully before the right operand runs, making it completely well-defined (unlike `i++ + arr[i]` which is Undefined Behavior). However, placing essential mutations like function calls or increments on the right-hand side of && or || creates subtle logic bugs if short-circuiting skips them: in `a = 1; (a || ++b);`, `b` is never incremented.
[HI] ISO C मानक && और || के पहले ऑपरेंड के मूल्यांकन के ठीक बाद एक सीक्वेंस पॉइंट सुनिश्चित करता है। इसी कारण `i++ < n && arr[i] > 0` जैसे एक्सप्रेशन में बाएँ भाग का साइड-इफेक्ट दायाँ भाग चलने से पहले ही पूरा हो जाता है और कोई अनडिफाइंड बिहेवियर नहीं होता। परंतु दाएँ ऑपरेंड में `++b` या कोई आवश्यक फंक्शन कॉल रखना खतरनाक हो सकता है क्योंकि शॉर्ट-सर्किट होने पर दायाँ भाग बिल्कुल निष्पादित नहीं होगा।
4. Precedence Hierarchy (! > && > ||) and Bitwise vs Logical Contrast (प्राथमिकता क्रम (! > && > ||) तथा बिटवाइज़ से अंतर)
[EN] Among logical operators, unary `!` binds tightest (unary precedence level 2), followed by relational/equality operators, then `&&` (level 11), and finally `||` (level 12), with `&&` and `||` associating left-to-right. Consequently, `1 || 0 && 0` parses as `1 || (0 && 0)` and evaluates to 1, NOT 0. Never confuse `&&` / `||` with bitwise `&` / `|`: `5 && 2` equals `1` (both non-zero), whereas `5 & 2` performs bit-level masking (`0101 & 0010`) and evaluates to `0`.
[HI] लॉजिकल ऑपरेटर्स में यूनरी `!` की प्राथमिकता सबसे अधिक होती है, उसके बाद रिलेशनल/इक्वैलिटी ऑपरेटर्स आते हैं, फिर `&&` और सबसे अंत में `||` आता है। इसी कारण `1 || 0 && 0` को `1 || (0 && 0)` मानकर हल किया जाता है जिससे उत्तर 1 मिलता है। लॉजिकल `&&` को बिटवाइज़ `&` समझने की भूल न करें: `5 && 2` का मान 1 (सत्य) होता है, जबकि `5 & 2` बिट स्तर पर `0101 & 0010` होकर 0 लौटाता है।
Syntax, Formula & Technical Example (Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation))
#include <stdio.h>
int main() {
int a = 0, b = 5, c = 1;
int r1 = (a && ++b);
int r2 = (c || ++b);
printf("r1=%d r2=%d b=%d !a=%d\n", r1, r2, b, !a);
return 0;
}
// Output: r1=0 r2=1 b=5 !a=1
Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation) Quick Reference & Comparison Table
| Logical Construct | Evaluation & Output Rule | Critical Exam Note |
|---|---|---|
| Logical AND (&&) | Skips RHS when LHS is 0; yields int 1 or 0 | Establishes sequence point after LHS evaluation |
| Logical OR (||) | Skips RHS when LHS is non-zero; yields int 1 or 0 | Binds looser than && in mixed expressions |
| Logical NOT (!) | Unary operator turning 0 to 1 and non-zero to 0 | Highest precedence among logical operators |
| Double Negation (!!x) | Converts any non-zero scalar cleanly to int 1 | Normalizes negative or large scalars to 0/1 |
Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation) Top 20 Bilingual MCQs with Answers
Q1. What does `printf("%d", 10 && -5);` print in C?
(C में `printf("%d", 10 && -5);` क्या प्रिंट करेगा?)
(A) 0
(B) 1
(C) -5
(D) 10
Correct Answer: (B)
[EN] Both 10 and -5 are non-zero (truthy), and && always returns int 1 when both operands are truthy.
[HI] 10 और -5 दोनों गैर-शून्य (सत्य) हैं, तथा && सत्य होने पर सदैव int मान 1 ही लौटाता है।
Q2. What is the output of `int x = 0, y = 5; int z = (x && ++y); printf("%d %d", z, y);`?
(`int x = 0, y = 5; int z = (x && ++y); printf("%d %d", z, y);` का आउटपुट क्या होगा?)
(A) 0 5
(B) 1 6
(C) 0 6
(D) 1 5
Correct Answer: (A)
[EN] Because x is 0, && short-circuits immediately; ++y is never executed, leaving y = 5 and z = 0.
[HI] चूंकि x का मान 0 है, && शॉर्ट-सर्किट होकर ++y को चलाता ही नहीं है, अतः z = 0 और y = 5 बना रहता है।
Q3. What is the output of `int a = 2, b = 3; int r = (a || ++b); printf("%d %d", r, b);`?
(`int a = 2, b = 3; int r = (a || ++b); printf("%d %d", r, b);` का आउटपुट क्या होगा?)
(A) 3 4
(B) 1 3
(C) 2 3
(D) 1 4
Correct Answer: (B)
[EN] Since a (2) is non-zero, || short-circuits and skips ++b, returning int 1 while b stays 3.
[HI] a (2) के गैर-शून्य होने से || शॉर्ट-सर्किट करता है और ++b को छोड़कर सीधे 1 लौटाता है, जिससे b = 3 रहता है।
Q4. What does `printf("%d", 1 || 0 && 0);` print in C?
(C में `printf("%d", 1 || 0 && 0);` का आउटपुट क्या होगा?)
(A) Undefined
(B) 1
(C) Compile error
(D) 0
Correct Answer: (B)
[EN] && has higher precedence than ||, so the expression parses as 1 || (0 && 0) = 1 || 0 = 1.
[HI] && की प्राथमिकता || से अधिक होती है, इसलिए यह 1 || (0 && 0) के रूप में हल होकर 1 देता है।
Q5. What is the value of `printf("%d %d", !5, !!-99);` in C?
(C में `printf("%d %d", !5, !!-99);` का मान क्या होगा?)
(A) 1 0
(B) 0 1
(C) 0 -99
(D) 0 99
Correct Answer: (B)
[EN] !5 turns non-zero 5 into 0; !!-99 computes !(!-99) = !(0) = 1.
[HI] !5 से गैर-शून्य मान 0 बनता है; !!-99 में पहले !-99 से 0 और फिर !0 से 1 प्राप्त होता है।
Q6. Compare the C expressions `5 && 2` and `5 & 2`. What do they evaluate to respectively?
(C में `5 && 2` और `5 & 2` क्रमशः क्या मान देते हैं?)
(A) 2 and 0
(B) 0 and 1
(C) 1 and 0
(D) 1 and 1
Correct Answer: (C)
[EN] 5 && 2 is logical AND of two non-zero integers (1), whereas 5 & 2 is bitwise AND (0101 & 0010 = 0000 = 0).
[HI] 5 && 2 लॉजिकल AND होकर 1 देता है, जबकि 5 & 2 बिटवाइज़ AND (0101 & 0010 = 0000) होकर 0 देता है।
Q7. Why is `if (ptr != NULL && *ptr == 10)` completely safe from null-pointer dereference in C?
(C में `if (ptr != NULL && *ptr == 10)` नल-पॉइंटर क्रैश से पूरी तरह सुरक्षित क्यों रहता है?)
(A) Because *ptr returns 0 safely if ptr is NULL
(B) Because && guarantees left-to-right short-circuiting, skipping *ptr when ptr is NULL
(C) Because != has lower precedence than &&
(D) Because C checks all pointers automatically
Correct Answer: (B)
[EN] When ptr == NULL, the left side is 0, so && skips evaluating *ptr entirely.
[HI] जब ptr NULL होता है, तब बायाँ भाग असत्य (0) हो जाता है और शॉर्ट-सर्किटिंग के कारण *ptr को कभी पढ़ा ही नहीं जाता।
Q8. Which logical operators establish an ISO C sequence point between their left and right operands?
(इनमें से कौन से लॉजिकल ऑपरेटर्स अपने बाएँ व दाएँ ऑपरेंड के बीच ISO C सीक्वेंस पॉइंट स्थापित करते हैं?)
(A) && and ||
(B) ! and &&
(C) None of them
(D) Only !
Correct Answer: (A)
[EN] ISO C guarantees a sequence point after the first operand of && and || (as well as ?: and comma ,).
[HI] ISO C में && और || दोनों अपने पहले ऑपरेंड के पूरा होने पर सीक्वेंस पॉइंट बनाते हैं।
Q9. What does `printf("%d", !0.0);` evaluate to in C?
(C में `printf("%d", !0.0);` का मान क्या होता है?)
(A) 0.0
(B) 1
(C) 0
(D) Compile error
Correct Answer: (B)
[EN] Logical NOT ! accepts any scalar including floating-point values; 0.0 is false, so !0.0 returns int 1.
[HI] ! किसी भी स्केलर (फ्लोटिंग सहित) पर लागू होता है; 0.0 को असत्य मानकर !0.0 int 1 लौटाता है।
Q10. According to De Morgan's Law in C, `!(a > 5 && b < 10)` is logically equivalent to:
(डी मॉर्गन के नियम (De Morgan's Law) के अनुसार C में `!(a > 5 && b < 10)` किसके समतुल्य है?)
(A) a <= 5 || b >= 10
(B) a < 5 || b > 10
(C) a <= 5 && b >= 10
(D) !(a > 5) && !(b < 10)
Correct Answer: (A)
[EN] !(A && B) transforms into !A || !B; the negation of a > 5 is a <= 5, and !(b < 10) is b >= 10.
[HI] !(A && B) = !A || !B होता है; (a > 5) का निषेध (a <= 5) तथा (b < 10) का निषेध (b >= 10) होता है।
Q11. What is printed by `int x = 1, y = 1, z = 1; int r = ++x || ++y && ++z; printf("%d %d %d %d", r, x, y, z);`?
(`int x = 1, y = 1, z = 1; int r = ++x || ++y && ++z; printf("%d %d %d %d", r, x, y, z);` क्या प्रिंट करेगा?)
(A) 1 2 2 2
(B) 1 2 1 1
(C) 2 2 1 1
(D) 1 2 2 1
Correct Answer: (B)
[EN] Operator precedence groups it as ++x || (++y && ++z). ++x runs first giving 2 (non-zero), so || short-circuits the entire RHS (++y && ++z), leaving y=1 and z=1.
[HI] प्राथमिकता के कारण यह ++x || (++y && ++z) बनता है। ++x = 2 (सत्य) मिलते ही || पूरे दाएँ भाग (++y && ++z) को शॉर्ट-सर्किट कर देता है, जिससे y=1, z=1 रहते हैं।
Q12. Which precedence order from HIGHEST to LOWEST is correct for C?
(C में उच्चतम से निम्नतम प्राथमिकता (Precedence) का कौन सा क्रम सही है?)
(A) && > || > ! > ==
(B) ! > < > == > && > ||
(C) < > ! > && > ||
(D) || > && > == > !
Correct Answer: (B)
[EN] Unary ! is highest (tier 2), relational < is tier 6, equality == is tier 7, && is tier 11, and || is tier 12.
[HI] यूनरी ! सबसे उच्च है, उसके बाद रिलेशनल (<), फिर इक्वैलिटी (==), फिर && और अंत में || आता है।
Q13. If `int i = 0;`, is the expression `(i++ == 0 && i == 1)` well-defined in C, and what is its value?
(यदि `int i = 0;` हो, तो क्या `(i++ == 0 && i == 1)` C में वैध (Well-defined) है और इसका मान क्या होगा?)
(A) Compile error
(B) Well-defined and evaluates to 0
(C) Undefined Behavior due to modifying and reading i
(D) Yes, well-defined and evaluates to 1
Correct Answer: (D)
[EN] && introduces a sequence point after evaluating (i++ == 0); at that point i++ was 0 (true) and i becomes 1 before i == 1 runs.
[HI] && बाएँ हिस्से के बाद सीक्वेंस पॉइंट बनाता है; पहले भाग में i++ == 0 सत्य (0==0) होता है और i बढ़कर 1 बन जाता है, फिर i == 1 भी सत्य (1) होता है।
Q14. In `(a == 0 || b / a > 2)` when `a = 0` and `b = 10`, what happens at runtime?
(जब `a = 0` और `b = 10` हो, तो `(a == 0 || b / a > 2)` के रनटाइम निष्पादन में क्या होगा?)
(A) Crashes with division-by-zero exception
(B) Evaluates safely to 1 without dividing by zero
(C) Evaluates to 0
(D) Triggers compile warning and halts
Correct Answer: (B)
[EN] a == 0 evaluates to 1, causing || to short-circuit and completely skip b / a.
[HI] a == 0 सत्य (1) होने से || शॉर्ट-सर्किट हो जाता है और b / a की गणना बिल्कुल नहीं की जाती, जिससे क्रैश बच जाता है।
Q15. Which statement is NOT true about logical operators in C?
(C में लॉजिकल ऑपरेटर्स के बारे में इनमें से कौन सा कथन असत्य है?)
(A) Negative numbers like -10 count as true in logical expressions
(B) Logical operators && and || always evaluate both operands before deciding truth value
(C) ! is a unary operator with right-to-left associativity
(D) The result of &&, ||, and ! is always integer 0 or 1
Correct Answer: (B)
[EN] && and || use short-circuit evaluation and skip the right operand whenever the left operand decides the result.
[HI] && और || शॉर्ट-सर्किट मूल्यांकन करते हैं और बाएँ ऑपरेंड से उत्तर तय होते ही दाएँ ऑपरेंड को नहीं जांचते।
Q16. What does `printf("%d", !"false");` print in C?
(C में `printf("%d", !"false");` क्या प्रिंट करेगा?)
(A) Compile error
(B) Garbage value
(C) 1
(D) 0
Correct Answer: (D)
[EN] A string literal decays to its non-NULL base memory address, which is non-zero (truthy), so !"false" evaluates to 0.
[HI] स्ट्रिंग लिटरल "false" अपने गैर-शून्य मेमोरी पते में बदलता है जिसे सत्य माना जाता है, अतः !"false" का मान 0 प्रिंट होता है।
Q17. Within Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation), which logical construct directly maps to: "Skips RHS when LHS is 0; yields int 1 or 0"?
(Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation) में कौन सा Logical Construct सीधे "Skips RHS when LHS is 0; yields int 1 or 0" से संबंधित है?)
(A) Logical OR (||)
(B) Double Negation (!!x)
(C) Logical AND (&&)
(D) Logical NOT (!)
Correct Answer: (C)
[EN] In Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation), 'Logical AND (&&)' handles 'Skips RHS when LHS is 0; yields int 1 or 0' (Establishes sequence point after LHS evaluation).
[HI] Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation) में 'Logical AND (&&)' का कार्य/विवरण 'Skips RHS when LHS is 0; yields int 1 or 0' (Establishes sequence point after LHS evaluation) है।
Q18. Which option accurately describes 'Logical OR (||)' in Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation)?
(Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation) के संदर्भ में 'Logical OR (||)' की सही विशेषता कौन सी है?)
(A) Skips RHS when LHS is 0; yields int 1 or 0
(B) Unary operator turning 0 to 1 and non-zero to 0
(C) Skips RHS when LHS is non-zero; yields int 1 or 0
(D) Converts any non-zero scalar cleanly to int 1
Correct Answer: (C)
[EN] 'Logical OR (||)' provides 'Skips RHS when LHS is non-zero; yields int 1 or 0' — key point: Binds looser than && in mixed expressions.
[HI] 'Logical OR (||)' का मुख्य विवरण 'Skips RHS when LHS is non-zero; yields int 1 or 0' है (Binds looser than && in mixed expressions)।
Q19. In Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation), identify the correct parameter for the specification: "Unary operator turning 0 to 1 and non-zero to 0".
(Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation) में "Unary operator turning 0 to 1 and non-zero to 0" विवरण किस विकल्प के लिए सही है?)
(A) Logical OR (||)
(B) Logical AND (&&)
(C) Logical NOT (!)
(D) Double Negation (!!x)
Correct Answer: (C)
[EN] 'Logical NOT (!)' corresponds to 'Unary operator turning 0 to 1 and non-zero to 0' (Highest precedence among logical operators).
[HI] 'Logical NOT (!)' का संबंध 'Unary operator turning 0 to 1 and non-zero to 0' (Highest precedence among logical operators) से है।
Q20. Considering Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation), what key behavior defines 'Double Negation (!!x)'?
(Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation) में 'Double Negation (!!x)' की मुख्य भूमिका या मान क्या है?)
(A) Converts any non-zero scalar cleanly to int 1
(B) Skips RHS when LHS is non-zero; yields int 1 or 0
(C) Skips RHS when LHS is 0; yields int 1 or 0
(D) Unary operator turning 0 to 1 and non-zero to 0
Correct Answer: (A)
[EN] 'Double Negation (!!x)' represents 'Converts any non-zero scalar cleanly to int 1' (Normalizes negative or large scalars to 0/1).
[HI] 'Double Negation (!!x)' का अर्थ/मान 'Converts any non-zero scalar cleanly to int 1' है (Normalizes negative or large scalars to 0/1)।
Logical Operators in C: &&, ||, ! (Short-Circuit Evaluation) — Exam FAQs
Q1: What is the key difference between logical (&&, ||) and bitwise (&, |) operators?
लॉजिकल (&&, ||) और बिटवाइज़ (&, |) ऑपरेटर्स में मुख्य अंतर क्या है?
[EN] Logical operators treat operands as whole truth values (0 vs non-zero), always return 0 or 1, and perform short-circuit evaluation with a sequence point. Bitwise operators work independently on each bit of integers, evaluate both operands unconditionally, and return an integer bitmask.
[HI] लॉजिकल ऑपरेटर्स पूरे मान को सत्य/असत्य मानकर 0 या 1 लौटाते हैं और शॉर्ट-सर्किटिंग करते हैं। बिटवाइज़ ऑपरेटर्स दोनों ऑपरेंड की जांच कर प्रत्येक बिट पर अलग-अलग ऑपरेशन करते हैं और एक बिटमास्क पूर्णांक लौटाते हैं।
Q2: Why does `1 || 0 && 0` evaluate to 1 instead of 0?
`1 || 0 && 0` का परिणाम 0 के बजाय 1 क्यों आता है?
[EN] Because `&&` has higher operator precedence than `||` in C, the compiler groups the expression as `1 || (0 && 0)`. Since the left side of `||` is 1, the right group is skipped and the result is 1.
[HI] C में `&&` की प्राथमिकता `||` से अधिक होती है, इसलिए यह `1 || (0 && 0)` के रूप में हल होकर 1 देता है।
Q3: When is using `!!x` useful in C programming?
C प्रोग्रामिंग में `!!x` (डबल निगेशन) का उपयोग कब उपयोगी होता है?
[EN] `!!x` converts any non-zero scalar (e.g., 42, -3, or a non-NULL pointer) strictly into `1` while keeping `0` as `0`. It is useful when storing pure boolean flags in bit-fields or summing matched conditions.
[HI] `!!x` किसी भी गैर-शून्य संख्या या पॉइंटर को सख्ती से `1` में और `0` को `0` में बदल देता है, जो बिट-फील्ड फ्लैग्स या गिनती करने में बहुत काम आता है।
Post a Comment
Post a Comment