Logical Operators in C Language (C लैंग्वेज में लॉजिकल ऑपरेटर्स)
Logical operators are used to perform logical operations on operands or expressions. They are generally used to combine two or more conditions or reverse the result of a condition to produce a final logical result. In C language, logical operators return 1 (true) or 0 (false).
लॉजिकल ऑपरेटर्स का उपयोग ऑपरेंड्स अथवा एक्सप्रेशन्स पर तार्किक संक्रिया करने के लिए किया जाता है। सामान्यतः इनका उपयोग दो या दो से अधिक कंडीशन्स को आपस में जोड़ने अथवा किसी कंडीशन के परिणाम को उलटने के लिए किया जाता है।
C लैंग्वेज में लॉजिकल ऑपरेटर्स का परिणाम 1 (True) या 0 (False) होता है।
Let A = 1 and B = 0. माना कि A = 1 तथा B = 0 हैं।
Types of Logical Operators in C (C में लॉजिकल ऑपरेटर्स के प्रकार)
C language provides three logical operators. The following table represents these operators, their descriptions, and examples.
C लैंग्वेज में तीन लॉजिकल ऑपरेटर्स उपलब्ध हैं। निम्न तालिका में इनके ऑपरेटर्स, संक्षिप्त विवरण तथा उदाहरण दिए गए हैं।
| Operator | Description (विवरण) | Example |
|---|---|---|
&& | Logical AND Operator (तार्किक AND ऑपरेटर) | (A && B) → False (0) |
|| | Logical OR Operator (तार्किक OR ऑपरेटर) | (A || B) → True (1) |
! | Logical NOT Operator (तार्किक NOT ऑपरेटर) | !B → True (1) |
1. Logical AND Operator (&&) (लॉजिकल AND ऑपरेटर)
The Logical AND operator && returns true (1) only when both conditions are true. If any one or both conditions are false, it returns false (0).
Logical AND ऑपरेटर && का परिणाम तभी True (1) होता है जब दोनों कंडीशन्स सत्य हों। यदि एक या दोनों कंडीशन्स असत्य होती हैं, तो इसका परिणाम False (0) होता है।
Example:
#include <stdio.h>int main(void){int A = 1, B = 0;printf("A && B = %d", A && B);return 0;}
Output:
A && B = 0Explanation:
Here, A is true (1) and B is false (0). Since both conditions are not true, the result of Logical AND is false (0).
व्याख्या:
यहाँ A सत्य (1) तथा B असत्य (0) है। चूँकि दोनों कंडीशन्स सत्य नहीं हैं, इसलिए Logical AND का परिणाम असत्य (0) होगा।
2. Logical OR Operator (||) (लॉजिकल OR ऑपरेटर)
The Logical OR operator || returns true (1) if at least one condition is true. It returns false (0) only when both conditions are false.
Logical OR ऑपरेटर || का परिणाम True (1) होता है यदि कम से कम एक कंडीशन सत्य हो। इसका परिणाम False (0) केवल तभी होता है जब दोनों कंडीशन्स असत्य हों।
Example:
#include <stdio.h>int main(void){int A = 1, B = 0;printf("A || B = %d", A || B);return 0;}
Output:
A || B = 1Explanation:
Here, A is true (1) and B is false (0). Since at least one condition is true, the result of Logical OR is true (1).
व्याख्या:
यहाँ A सत्य (1) तथा B असत्य (0) है। चूँकि कम से कम एक कंडीशन सत्य है, इसलिए Logical OR का परिणाम सत्य (1) होगा।
3. Logical NOT Operator (!) (लॉजिकल NOT ऑपरेटर)
The Logical NOT operator ! is a unary operator. It reverses the logical result of an expression. If the expression is true (1), it returns false (0), and if the expression is false (0), it returns true (1).
Logical NOT ऑपरेटर ! एक यूनरी ऑपरेटर है। यह किसी एक्सप्रेशन के तार्किक परिणाम को उलट देता है। यदि एक्सप्रेशन सत्य (1) है, तो यह असत्य (0) देता है तथा यदि एक्सप्रेशन असत्य (0) है, तो यह सत्य (1) देता है।
Example:
#include <stdio.h>int main(void){int B = 0;printf("!B = %d", !B);return 0;}
Output:
!B = 1Explanation:
Here, B is false (0). The NOT operator reverses its result and returns true (1).
व्याख्या:
यहाँ B असत्य (0) है। NOT ऑपरेटर इसके परिणाम को उलटकर सत्य (1) कर देता है।
Truth Table of Logical Operators (लॉजिकल ऑपरेटर्स की ट्रुथ टेबल)
A truth table shows all possible input combinations and their corresponding logical results.
ट्रुथ टेबल में सभी संभावित इनपुट कॉम्बिनेशन्स तथा उनके अनुरूप लॉजिकल परिणाम दर्शाए जाते हैं।
Logical AND (&&)
| A | B | A && B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Rule: The result is true only when both operands are true.
नियम: परिणाम तभी सत्य होता है जब दोनों ऑपरेंड सत्य हों।
Logical OR (||)
| A | B | A || B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Rule: The result is false only when both operands are false.
नियम: परिणाम तभी असत्य होता है जब दोनों ऑपरेंड असत्य हों।
Logical NOT (!)
| A | !A |
|---|---|
| 0 | 1 |
| 1 | 0 |
Rule: The NOT operator reverses the logical value.
नियम: NOT ऑपरेटर लॉजिकल मान को उलट देता है।
C Program to Demonstrate Logical Operators
लॉजिकल ऑपरेटर्स को प्रदर्शित करने वाला C प्रोग्राम
The following program demonstrates all three logical operators using two integer variables.
निम्न प्रोग्राम दो Integer Variables की सहायता से तीनों लॉजिकल ऑपरेटर्स को प्रदर्शित करता है।
Program:
#include <stdio.h>int main(void){int A = 1, B = 0;printf("Logical AND = %d\n", A && B);printf("Logical OR = %d\n", A || B);printf("Logical NOT = %d\n", !B);return 0;}
Output:
Logical AND = 0Logical OR = 1Logical NOT = 1
Explanation:
A && Breturns0because both operands are not true.A || Breturns1because at least one operand is true.!Breturns1because NOT reverses the value of B.
व्याख्या:
A && Bका परिणाम0है क्योंकि दोनों ऑपरेंड सत्य नहीं हैं।A || Bका परिणाम1है क्योंकि कम से कम एक ऑपरेंड सत्य है।!Bका परिणाम1है क्योंकि NOT ऑपरेटर B के मान को उलट देता है।
Practical Example of Logical Operators (लॉजिकल ऑपरेटर्स का व्यावहारिक उदाहरण)
Logical operators are commonly used in decision-making statements to check multiple conditions.
लॉजिकल ऑपरेटर्स का उपयोग एक से अधिक कंडीशन्स की जाँच करने के लिए Decision-Making Statements में किया जाता है।
Example: Checking Student Eligibility
Suppose a student must score at least 40 marks in both subjects to pass.
मान लीजिए किसी विद्यार्थी को पास होने के लिए दोनों विषयों में कम से कम 40 अंक प्राप्त करना आवश्यक है।
#include <stdio.h>int main(void){int c_marks = 60;int cpp_marks = 45;if (c_marks >= 40 && cpp_marks >= 40){printf("Student is Pass");}else{printf("Student is Fail");}return 0;}
Output:
Student is PassExplanation:
Both conditions are true because the student has scored at least 40 marks in both subjects. Therefore, the Logical AND operator returns true.
व्याख्या:
दोनों कंडीशन्स सत्य हैं क्योंकि विद्यार्थी ने दोनों विषयों में कम से कम 40 अंक प्राप्त किए हैं। इसलिए Logical AND ऑपरेटर का परिणाम सत्य होता है।
Short-Circuit Evaluation in C (C में शॉर्ट-सर्किट मूल्यांकन)
C language uses short-circuit evaluation with Logical AND (&&) and Logical OR (||). It means that the second operand is evaluated only when necessary.
C लैंग्वेज में Logical AND (&&) तथा Logical OR (||) के साथ Short-Circuit Evaluation का उपयोग किया जाता है। इसका अर्थ है कि दूसरे ऑपरेंड का मूल्यांकन केवल तभी किया जाता है जब उसकी आवश्यकता होती है।
1. Short-Circuit Evaluation with AND (&&)
If the first condition is false, the second condition is not evaluated because the final result will always be false.
यदि पहली कंडीशन असत्य है, तो दूसरी कंडीशन का मूल्यांकन नहीं किया जाता क्योंकि अंतिम परिणाम हमेशा असत्य ही होगा।
2. Short-Circuit Evaluation with OR (||)
If the first condition is true, the second condition is not evaluated because the final result will always be true.
यदि पहली कंडीशन सत्य है, तो दूसरी कंडीशन का मूल्यांकन नहीं किया जाता क्योंकि अंतिम परिणाम हमेशा सत्य ही होगा।
Difference Between Logical AND and Bitwise AND
Logical AND तथा Bitwise AND में अंतर
| Basis | Logical AND (&&) | Bitwise AND (&) |
|---|---|---|
| Operation | Performs logical operation | Performs bit-by-bit operation |
| हिंदी | तार्किक संक्रिया | प्रत्येक बिट पर संक्रिया |
| Operands | Commonly used with conditions | Used with integer operands |
| Result | 0 or 1 | Integer bitwise result |
| Short-circuit | Yes | No |
Example:
int A = 5, B = 3;printf("%d\n", A && B);printf("%d\n", A & B);
Output:
11
Here, A && B returns 1 because both values are non-zero (true). A & B also happens to return 1, but it is calculated using bitwise AND:
0101& 0011------0001
Important Points for Exams (परीक्षाओं के लिए महत्वपूर्ण बातें)
- There are three logical operators in C:
&&,||, and!.C में तीन लॉजिकल ऑपरेटर्स होते हैं:&&,||तथा!। - Logical AND is represented by
&&.Logical AND को&&से दर्शाते हैं। - Logical OR is represented by
||.Logical OR को||से दर्शाते हैं। - Logical NOT is represented by
!.Logical NOT को!से दर्शाते हैं। - Logical AND (
&&) returns true only when both conditions are true.Logical AND (&&) तभी सत्य होता है जब दोनों कंडीशन्स सत्य हों। - Logical OR (
||) returns false only when both conditions are false.Logical OR (||) तभी असत्य होता है जब दोनों कंडीशन्स असत्य हों। - Logical NOT (
!) is a unary operator.Logical NOT (!) एक यूनरी ऑपरेटर है। - Logical AND (
&&) and Logical OR (||) are binary operators.Logical AND (&&) तथा Logical OR (||) बाइनरी ऑपरेटर्स हैं। - Logical operators return
1or0in C.C में लॉजिकल ऑपरेटर्स का परिणाम1या0होता है। - Logical operators are frequently used in
if,if...else, and loop conditions.लॉजिकल ऑपरेटर्स का उपयोगif,if...elseतथा लूप्स की कंडीशन में किया जाता है। - Logical AND and OR use short-circuit evaluation.Logical AND तथा OR में Short-Circuit Evaluation होता है।
Logical operators are an important part of C programming. They are used to combine multiple conditions, reverse logical results, and make decisions in programs. A clear understanding of logical operators is essential for learning conditional statements and loops.
लॉजिकल ऑपरेटर्स C प्रोग्रामिंग का एक महत्वपूर्ण भाग हैं। इनका उपयोग एक से अधिक कंडीशन्स को जोड़ने, तार्किक परिणाम को उलटने तथा प्रोग्राम में निर्णय लेने के लिए किया जाता है। Conditional Statements तथा Loops सीखने के लिए लॉजिकल ऑपरेटर्स की अच्छी समझ आवश्यक है।
Comments
Post a Comment