Operators in C++ क्या हैं? Types, Examples और आसान Explanation
जब किसी C++ program में हमें दो numbers को जोड़ना हो, किसी number को छोटा-बड़ा check करना हो, condition लगानी हो या किसी variable की value बदलनी हो, तब केवल variables काफी नहीं होते। इन कामों के लिए हमें Operators in C++ की जरूरत पड़ती है।
उदाहरण के लिए 10 + 20 में + एक Operator है। इसी तरह 10 > 5 में > Operator दो values की तुलना करता है। C++ programming सीखते समय Operators को अच्छी तरह समझना बहुत जरूरी है, क्योंकि लगभग हर practical program में इनका इस्तेमाल होता है।
अगर आप अभी C++ सीखना शुरू कर रहे हैं, तो Operators को किसी mathematical symbol की तरह समझ सकते हैं। फर्क सिर्फ इतना है कि C++ में इनका इस्तेमाल calculation के साथ-साथ comparison, logical decision, assignment और bit-level operations के लिए भी किया जाता है।
Operator और Operand क्या होते हैं?
किसी operation में जिस symbol का इस्तेमाल काम करने के लिए किया जाता है, उसे Operator कहते हैं और जिन values या variables पर operation होता है, उन्हें Operand कहते हैं।
10 + 20
+→ Operator10→ Operand20→ Operand
इस expression का result 30 होगा।
C++ में Operators के मुख्य प्रकार
C++ में अलग-अलग कामों के लिए कई प्रकार के Operators उपलब्ध हैं। सामान्य रूप से इन्हें इन categories में समझा जाता है:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment और Decrement Operators
- Bitwise Operators
- Conditional या Ternary Operator
- sizeof Operator
- Comma Operator
- Unary Operators
1. Arithmetic Operators in C++
Arithmetic Operators का इस्तेमाल mathematical calculations करने के लिए किया जाता है। ये सबसे basic और सबसे ज्यादा इस्तेमाल होने वाले Operators हैं।
| Operator | काम | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Remainder | a % b |
Example
#include <iostream>
using namespace std;
int main()
{
int a = 20, b = 6;
cout << "Addition = " << a + b << endl;
cout << "Subtraction = " << a - b << endl;
cout << "Multiplication = " << a * b << endl;
cout << "Division = " << a / b << endl;
cout << "Remainder = " << a % b << endl;
return 0;
}
ध्यान रखें कि अगर दोनों operands int हैं, तो division में integer result मिलेगा। जैसे 20 / 6 का result 3 होगा, decimal value नहीं।
2. Relational Operators
Relational Operators का इस्तेमाल दो values की तुलना करने के लिए होता है। इनका result सामान्यतः true या false होता है।
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example
int age = 18;
cout << (age >= 18);
यह expression check करता है कि age 18 या उससे अधिक है या नहीं। Condition सही होने पर result 1 या true के रूप में दिखाई दे सकता है।
Comparison का आसान Flow:
Value A → Comparison Operator → Value B → true / false
3. Logical Operators
जब program में एक से ज्यादा conditions को जोड़ना हो, तब Logical Operators उपयोगी होते हैं। इन्हें if, while और दूसरे decision-making statements में बहुत इस्तेमाल किया जाता है।
| Operator | Name | काम |
|---|---|---|
| && | Logical AND | दोनों conditions true होनी चाहिए |
| || | Logical OR | कम से कम एक condition true हो |
| ! | Logical NOT | Result को उलट देता है |
Real-life Example
मान लीजिए किसी student को exam में बैठने के लिए age 15 से अधिक और attendance 75% या उससे अधिक चाहिए।
if(age >= 15 && attendance >= 75)
{
cout << "Eligible";
}
यहाँ && का मतलब है कि दोनों conditions सही होनी चाहिए।
4. Assignment Operators
Assignment Operator का मुख्य काम किसी variable में value store करना है। इसका सबसे basic form = है।
int marks = 85;
यहाँ 85 को marks variable में store किया गया है।
C++ में कुछ shortcut assignment Operators भी होते हैं:
| Operator | Example | Equivalent |
|---|---|---|
| = | a = 10 | a = 10 |
| += | a += 5 | a = a + 5 |
| -= | a -= 5 | a = a - 5 |
| *= | a *= 5 | a = a * 5 |
| /= | a /= 5 | a = a / 5 |
| %= | a %= 5 | a = a % 5 |
5. Increment और Decrement Operators
किसी variable की value को 1 से बढ़ाने के लिए ++ और 1 से घटाने के लिए -- Operator का इस्तेमाल किया जाता है।
int x = 10;
x++;
cout << x; // 11
x--;
cout << x; // 10
Prefix और Postfix में अंतर
++x को Prefix Increment और x++ को Postfix Increment कहते हैं। Difference तब दिखाई देता है जब Operator किसी expression के अंदर इस्तेमाल किया जाता है।
int x = 5;
cout << ++x; // पहले value बढ़ेगी, फिर print होगी
int y = 5;
cout << y++; // पहले current value print होगी, फिर value बढ़ेगी
शुरुआत में इस concept को समझने का आसान तरीका है: Prefix में पहले operation, Postfix में पहले expression का current value वाला काम।
6. Bitwise Operators
Bitwise Operators numbers के binary bits पर operation करते हैं। ये सामान्य school-level calculations से थोड़ा अलग हैं और low-level programming, embedded systems तथा कुछ performance-oriented applications में उपयोगी हो सकते हैं।
| Operator | Name |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | Bitwise XOR |
| ~ | Bitwise NOT |
| << | Left Shift |
| >> | Right Shift |
Example
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
cout << (a & b);
Bitwise operation में calculation binary representation के bits पर होता है। इसलिए इसे समझने के लिए Binary Number System की basic knowledge मदद करती है।
7. Conditional या Ternary Operator
Ternary Operator का इस्तेमाल छोटी if-else condition को compact तरीके से लिखने के लिए किया जाता है। इसका syntax है:
condition ? value_if_true : value_if_false;
Example
int age = 20;
string result = (age >= 18) ? "Adult" : "Minor";
cout << result;
अगर condition true होगी तो पहला value मिलेगा और false होने पर दूसरा value मिलेगा।
8. sizeof Operator
sizeof का इस्तेमाल किसी data type या variable द्वारा memory में लिए गए size को जानने के लिए किया जाता है। इसका result bytes में मिलता है।
int age = 25;
cout << sizeof(age);
इसका exact result system और compiler के data type implementation पर निर्भर कर सकता है। इसलिए हर computer पर सभी data types का size एक जैसा मान लेना सही नहीं है।
9. Comma Operator
Comma Operator , एक ही expression में multiple expressions को sequence में लिखने की सुविधा देता है। इसका result सामान्यतः सबसे आखिरी expression का result होता है।
int x;
x = (10, 20, 30);
cout << x;
Output होगा:
30
C++ Operators का छोटा सा Diagram
C++ Operators
↓
Arithmetic → + - * / %
Relational → == != > < >= <=
Logical → && || !
Assignment → = += -= *= /= %=
Increment/Decrement → ++ --
Bitwise → & | ^ ~ << >>
Conditional → ? :
Other → sizeof, comma
Operators की Precedence और Associativity
जब एक expression में कई Operators होते हैं, तो C++ को यह तय करना पड़ता है कि पहले कौन-सा operation किया जाए। इसी नियम को Operator Precedence कहते हैं।
int result = 10 + 5 * 2;
यहाँ multiplication पहले होगा और फिर addition। इसलिए result 20 होगा, 30 नहीं।
अगर आपको operation का order स्पष्ट रखना है, तो parentheses का इस्तेमाल करें:
int result = (10 + 5) * 2;
अब addition पहले होगा और result 30 होगा।
एक Practical C++ Program में Operators का उपयोग
नीचे एक छोटा program है जो student के marks के आधार पर percentage और result check करता है। इसमें Arithmetic, Relational और Logical Operators का उपयोग किया गया है।
#include <iostream>
using namespace std;
int main()
{
int math = 80;
int science = 75;
int computer = 90;
int total = math + science + computer;
float percentage = total / 3.0;
cout << "Total Marks = " << total << endl;
cout << "Percentage = " << percentage << "%" << endl;
if(math >= 33 && science >= 33 && computer >= 33)
{
cout << "Result = Pass";
}
else
{
cout << "Result = Fail";
}
return 0;
}
Operators सीखते समय होने वाली Common Mistakes
1. = और == को एक जैसा समझना
= assignment के लिए और == comparison के लिए इस्तेमाल होता है।
x = 10; // Assignment
x == 10; // Comparison
2. Integer Division को भूल जाना
int a = 5;
int b = 2;
cout << a / b;
यहाँ result 2 होगा क्योंकि दोनों values integer हैं। Decimal result के लिए floating-point value का इस्तेमाल करें।
3. % Operator को decimal numbers पर इस्तेमाल करना
Modulo Operator का सामान्य उपयोग integer remainder के लिए होता है। इसलिए beginners को इसे integer values के साथ समझना सबसे आसान रहता है।
4. Prefix और Postfix में confusion
++x और x++ हमेशा समान तरीके से behave नहीं करते, खासकर complex expressions में। ऐसे expressions को unnecessarily complicated बनाने से बचें।
Expert Tips for C++ Operators
- Complex expression को समझने के लिए parentheses का उपयोग करें।
=और==का difference अच्छी तरह याद रखें।- Integer division को ध्यान में रखें।
- Logical Operators को truth table के साथ practice करें।
- Bitwise Operators सीखने से पहले Binary Number System revise करें।
- एक ही line में बहुत सारे increment/decrement operations लिखने से बचें।
- Operator precedence याद करने के बजाय जरूरत पड़ने पर parentheses का उपयोग करना बेहतर readability देता है।
Key Points – Quick Notes
- Operator किसी operation को perform करने वाला symbol या operator construct है।
- Operands वे values या variables हैं जिन पर operation किया जाता है।
+,-,*,/Arithmetic Operators हैं।==,!=,>,<Relational Operators हैं।&&,||,!Logical Operators हैं।=,+=,-=Assignment Operators हैं।++और--value को 1 से बढ़ाते या घटाते हैं।- Bitwise Operators binary bits पर काम करते हैं।
? :Ternary या Conditional Operator है।sizeofmemory size को bytes में जानने में मदद करता है।
Practical Exercises
- दो numbers लेकर उनका sum, difference, multiplication और division निकालें।
- एक number even है या odd, यह
%Operator से check करें। - दो numbers में से बड़ा number Relational Operator की मदद से find करें।
- एक student की age और marks देखकर eligibility check करें।
- Prefix और Postfix Increment का output अलग-अलग programs में observe करें।
- दो integers पर Bitwise AND, OR और XOR का प्रयोग करें।
- Ternary Operator से number positive या negative check करने का program बनाएं।
Interview Questions on C++ Operators
- C++ में Operator क्या है?
Operator वह symbol या operator construct है जो किसी operation को perform करता है। - Operator और Operand में क्या difference है?
Operator operation बताता है, जबकि Operand वह value या variable होता है जिस पर operation किया जाता है। - = और == में क्या अंतर है?
=assignment के लिए और==equality comparison के लिए होता है। - ++ और -- का क्या उपयोग है?
ये किसी value को क्रमशः 1 से बढ़ाने और घटाने के लिए इस्तेमाल होते हैं। - && और || में क्या difference है?
&&में सभी आवश्यक conditions true होनी चाहिए, जबकि||में कम से कम एक condition true होने पर पूरा logical expression true हो सकता है। - Ternary Operator क्या है?
यह तीन operands वाला conditional operator है, जिसका syntaxcondition ? true_value : false_valueहोता है। - sizeof Operator क्या करता है?
यह किसी type या object द्वारा उपयोग किए जा रहे memory size को bytes में बताता है।
MCQs – C++ Operators
Q1. Addition के लिए कौन-सा Operator इस्तेमाल होता है?
- A) *
- B) +
- C) %
- D) /
Answer: B) +
Q2. Equality check करने के लिए कौन-सा Operator है?
- A) =
- B) ==
- C) !=
- D) >=
Answer: B) ==
Q3. Logical AND Operator कौन-सा है?
- A) ||
- B) !
- C) &&
- D) &
Answer: C) &&
Q4. Remainder निकालने के लिए कौन-सा Operator उपयोग होता है?
Answer: %
Q5. Conditional Operator का सही form कौन-सा है?
Answer: ? :
Quick Quiz
1. अगर a = 10 और b = 3 है, तो a % b का result क्या होगा?
2. ++x को Prefix कहा जाता है या Postfix?
3. दो conditions को AND करने के लिए कौन-सा Operator इस्तेमाल होता है?
4. != का क्या मतलब है?
5. ? : को किस नाम से जाना जाता है?
Quiz Answers: 1) 1 2) Prefix 3) && 4) Not Equal 5) Ternary/Conditional Operator
Frequently Asked Questions (FAQs)
1. C++ में Operators क्या होते हैं?
C++ में Operators ऐसे symbols या constructs हैं जिनका इस्तेमाल calculation, comparison, logical decision, assignment और दूसरे operations के लिए किया जाता है। उदाहरण के लिए + addition और > comparison के लिए उपयोग होता है।
2. C++ में कितने प्रकार के Operators होते हैं?
C++ में Arithmetic, Relational, Logical, Assignment, Increment/Decrement, Bitwise, Conditional और दूसरे कई Operators होते हैं। C++ में कुछ Operators का behavior context के अनुसार बदल भी सकता है, इसलिए केवल एक fixed count याद करना जरूरी नहीं है।
3. = और == में क्या अंतर है?
= किसी variable को value assign करता है, जबकि == यह check करता है कि दो values बराबर हैं या नहीं। Beginners की सबसे common mistakes में यह confusion शामिल है।
4. % Operator का क्या उपयोग है?
% Modulus Operator है। इसका उपयोग division के बाद बचा हुआ remainder निकालने के लिए किया जाता है। उदाहरण के लिए 17 % 5 का result 2 होगा।
5. ++x और x++ में क्या अंतर है?
++x Prefix Increment है और x++ Postfix Increment। जब इन्हें expression में इस्तेमाल किया जाता है, तो दोनों में operation के evaluation order के कारण अलग result दिखाई दे सकता है।
6. Logical AND और Bitwise AND क्या एक ही हैं?
नहीं। && Logical AND है और conditions के साथ काम करता है, जबकि & Bitwise AND है और binary bits पर operation करता है। दोनों को एक जैसा समझना सही नहीं है।
7. Ternary Operator का इस्तेमाल क्यों किया जाता है?
Ternary Operator छोटी conditional situations में compact code लिखने के लिए उपयोगी है। हालांकि बहुत complex condition होने पर normal if-else ज्यादा readable हो सकता है।
8. क्या Operators C++ programming में जरूरी हैं?
हाँ। Basic calculation से लेकर conditions, loops, assignments और advanced programming तक Operators का लगातार इस्तेमाल होता है। इसलिए C++ सीखने वाले student को इनके syntax के साथ-साथ इनके practical behavior को भी समझना चाहिए।
Conclusion
Operators in C++ programming की basic building blocks में से एक हैं। शुरुआत में +, -, * और / जैसे Arithmetic Operators आसान लगते हैं, लेकिन जैसे-जैसे programming आगे बढ़ती है, Relational, Logical, Assignment, Bitwise और Conditional Operators की importance बढ़ती जाती है।
Operators को सिर्फ definitions याद करके सीखने के बजाय छोटे-छोटे C++ programs बनाकर practice करें। खासकर = और ==, && और &, तथा Prefix और Postfix जैसे concepts को खुद run करके देखने से confusion जल्दी दूर होता है।
अगर आपने इस topic को पढ़ लिया है, तो अब सबसे अच्छा अगला कदम यही है कि ऊपर दिए गए practical exercises को बिना solution देखे खुद लिखने की कोशिश करें। जिस Operator पर सबसे ज्यादा confusion हो, उसे comment में लिख सकते हैं—उसी concept को एक अलग example से समझना आसान रहेगा।

टिप्पणियाँ
एक टिप्पणी भेजें