अगर आप C++ programming सीखना शुरू कर चुके हैं या करने वाले हैं, तो सबसे पहले जिस chapter को आपको strong बनाना चाहिए वह है Operators in CPP । बिना operators समझे आप कोई भी calculation, decision making, loop condition या advanced logic कभी सही से नहीं बना पाएँगे।
इस लेख में आप सीखेंगे – operators क्या होते हैं, कितने types के होते हैं, हर operator कैसे काम करता है, और real C++ programs with examples। यह article specially beginners के लिए बनाया गया है ताकि आप 0 level से expert level तक आसानी से पहुँच सकें।
Operators क्या हैं? (What are Operators in C++)
Operators in C++ वे special symbols होते हैं जो किसी value या variable पर कोई operation perform करते हैं।
जैसे –
int a = 10, b = 5; int c = a + b;
यहाँ + एक operator है जो a और b को add कर रहा है।
Operand और Operator में Difference
Programming में किसी भी calculation या operation के लिए दो चीजें बहुत जरूरी होती हैं – Operator और Operand।
Operator वह special symbol होता है जो किसी value या variable पर कोई काम (operation) करता है।
Example Operators:
+ , – , * , / , % , = , > , < , &&
Example:
int c = a + b;
यहाँ + और = दोनों operators हैं।
Operand वे values या variables होते हैं जिन पर operator अपना काम करता है।
Example:
int c = a + b;
यहाँ a, b और c operands हैं क्योंकि operators इन्हीं पर operation perform कर रहे हैं।
| Operator | Operand |
| Operation perform करता है | जिस पर operation होता है |
| जैसे +, -, *, / | जैसे a, b, 10, 20 |
Types of Operators in C++
C++ में मुख्य रूप से निम्नलिखित operators होते हैं:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Ternary Operator
- Increment / Decrement Operators
- Special / Other Operators
अब हम सभी को detail में सीखेंगे।
Arithmetic Operators in C++
Arithmetic Operators in CPP का उपयोग mathematical calculation करने के लिए किया जाता है। इन operators से हम addition, subtraction, multiplication, division और remainder जैसे काम करते हैं। +, -, *, / और % जैसे symbols arithmetic operators के example हैं। इनके बिना किसी भी program में calculation संभव नहीं है।
| Operator | Meaning |
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Reminder) |
| ++ | Increment |
| — | Decrement |
Example Program –Arithmetic Operators in C++
#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 << "Modulus: " << a % b << endl;
return 0;
}
Assignment Operators in CPP
Assignment Operators का उपयोग किसी variable में value assign करने के लिए किया जाता है। सबसे common assignment operator = होता है। इसके अलावा +=, -=, *=, /= जैसे operators variable की current value में बदलाव करने के लिए प्रयोग होते हैं। ये operators coding को short और fast बनाते हैं।
| Operator | Example | Meaning |
| = | A=10 | value assign |
| += | A+=5 | a = a+5 |
| -= | A-= 5 | a = a-5 |
| *= | A*=2 | a = a * 2 |
| /= | a/=2 | a = a/2 |
Example Program – Assignment Operators in C++
#include <iostream>
using namespace std;
int main() {
int a = 10;
a += 5; // a = a + 5
cout << "After += : " << a << endl;
a -= 3; // a = a - 3
cout << "After -= : " << a << endl;
a *= 2; // a = a * 2
cout << "After *= : " << a << endl;
a /= 4; // a = a / 4
cout << "After /= : " << a << endl;
return 0;
}
Relational Operators in CPP
Relational Operators दो values के बीच comparison करने के लिए use होते हैं। ये हमेशा result true या false में देते हैं। जैसे ==, !=, <, >, <=, >=। इनका use conditions और decision making में किया जाता है, जैसे if-else statements।
| Operator | Meaning |
| == | Equal to |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
Example Program – Relational Operators in C++
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << "a == b : " << (a == b) << endl;
cout << "a != b : " << (a != b) << endl;
cout << "a > b : " << (a > b) << endl;
cout << "a < b : " << (a < b) << endl;
cout << "a >= b : " << (a >= b) << endl;
cout << "a <= b : " << (a <= b) << endl;
return 0;
}
Logical Operators in CPP
Logical Operators multiple conditions को combine करने के लिए काम आते हैं। && (AND), || (OR) और ! (NOT) logical operators हैं। इनका use तब किया जाता है जब program में दो या उससे अधिक conditions को check करना हो।
| Operator | Meaning |
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
Example Program – Logical Operators in CPP
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
if (a < b && b > 5)
cout << "AND operator is true" << endl;
if (a > b || b > 5)
cout << "OR operator is true" << endl;
if (!(a > b))
cout << "NOT operator is true" << endl;
return 0;
}
Bitwise Operators in C++
Bitwise Operators binary level पर काम करते हैं। ये operators bits पर operation perform करते हैं, जैसे &, |, ^, <<, >>, ~। इनका use low-level programming, data encryption और hardware related programs में किया जाता है।
| Operator | Meaning |
| & | AND |
| ^ | XOR |
| << | Left shift |
| >> | Right shift |
| ~ | NOT |
Example Program – Bitwise Operators in C++
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
cout << "a & b : " << (a & b) << endl;
cout << "a | b : " << (a | b) << endl;
cout << "a ^ b : " << (a ^ b) << endl;
cout << "a << 1: " << (a << 1) << endl;
cout << "a >> 1: " << (a >> 1) << endl;
return 0;
}
Ternary Operator in CPP
Ternary Operator को conditional operator भी कहा जाता है। यह if-else का short version होता है और इसमें तीन parts होते हैं – condition, true part और false part। यह code को छोटा और readable बनाता है।
Syntax:
(condition) ? expression1 : expression2;
Example Program – Ternary Operator in C++
#include <iostream>
using namespace std;
int main() {
int a = 15, b = 20;
int max = (a > b) ? a : b;
cout << "Maximum value is: " << max << endl;
return 0;
}
Increment और Decrement Operators
Increment और Decrement Operators का use किसी variable की value को 1 से बढ़ाने या घटाने के लिए किया जाता है। ++ increment के लिए और — decrement के लिए use होता है। ये operators loop और counting में बहुत helpful होते हैं।
| Operator | Meaning |
| ++a | Pre increment |
| a++ | Post increment |
| –a | Pre decrement |
| a– | Post decrement |
Example Program – Increment / Decrement Operators
#include <iostream>
using namespace std;
int main() {
int a = 10;
cout << "Post Increment: " << a++ << endl;
cout << "After Post Increment: " << a << endl;
cout << "Pre Increment: " << ++a << endl;
cout << "Post Decrement: " << a-- << endl;
cout << "After Post Decrement: " << a << endl;
return 0;
}
Special Other Operators in C++
Special Operators में sizeof, comma operator, pointer operators * और & जैसे symbols आते हैं। ये memory handling, address finding और data size जानने के लिए use होते हैं। ये operators advanced C++ programming में बहुत जरूरी भूमिका निभाते हैं।
| Operator | Use |
| sizeof | Size find करने के लिए |
| , | Multiple expressions |
| * | Pointer |
| & | Address |
Example Program – Special / Other Operators
#include <iostream>
using namespace std;
int main() {
int a = 10;
int *ptr = &a;
cout << "Value of a: " << a << endl;
cout << "Address of a: " << &a << endl;
cout << "Value using pointer: " << *ptr << endl;
cout << "Size of int: " << sizeof(a) << endl;
return 0;
}
Operator Precedence & Associativity
Operator Precedence और Associativity यह तय करते हैं कि C++ program में जब एक से ज्यादा operators एक साथ हों तो कौन-सा operator पहले execute होगा और किस order में calculation होगी।
Operator Precedence (Priority)
Operator Precedence का मतलब होता है – किसी expression में किस operator की priority ज्यादा है।
जिस operator की precedence ज्यादा होती है, वही पहले evaluate होता है।
Associativity
जब दो operators की precedence same होती है, तब Associativity decide करती है कि calculation left से होगी या right से।
| Operator | Priority |
| () | Highest |
| *, /, % | High |
| +, – | Medium |
| + | Low |
Example Program
int result = 10 + 5 * 2; // Output 20
Example Program Using Multiple Operators
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
if(a < b && b > 15) {
cout << "Logical Operator Working";
}
return 0;
}
यह भी पढ़ें : Encapsulation in C++ Programming क्या है? Easy & Complete Guide with Examples for Beginners
निष्कर्ष (Conclusion)
इस article में आपने Operators in CPP को detail में सीखा – types, examples, programs और rules के साथ। अब आप confidently calculations, decisions और loops बना सकते हैं। रोज practice करें, programs खुद से लिखें और C++ में master बनें।
अगर आपको यह guide helpful लगी हो, तो इसे bookmark करें और daily coding शुरू करें।
FAQs
Q1. Unary, Binary और Ternary Operator क्या होते हैं?
Ans.
Unary = एक operand
Binary = दो operand
Ternary = तीन operand
Q2. सबसे ज्यादा use होने वाला operator कौन सा है?
Ans. Assignment और Arithmetic operators।
Q3. Operator precedence क्यों जरूरी है?
Ans. ताकि program सही order में execute हो।
यह भी पढ़ें : राष्ट्रीय ग्रामीण आजीविका मिशन | National Rural Livelihood Mission Yojana: Benefits, Eligibility and how to apply