Operators in C++
The different categories of operators C++ provides for calculations and comparisons.
Operators in C++
Operators वे special symbols (विशेष चिन्ह) हैं जिनका उपयोग C++ में variables और values पर विभिन्न operations (क्रियाएँ) करने के लिए किया जाता है। Operators की सहायता से arithmetic calculations, comparison, logical operations, assignment आदि किए जा सकते हैं।
जिस data या value पर operator कार्य करता है उसे operand कहा जाता है।
a + b में + operator है तथा a और b operands हैं।
What is an Operator?
Operator एक symbol या keyword होता है जो एक या अधिक operands पर कोई specific operation perform करता है।
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 5;
cout << a + b;
return 0;
}
15
यहाँ + arithmetic operator है और a तथा b इसके operands हैं।
Types of Operators in C++
C++ में विभिन्न प्रकार के operators उपलब्ध हैं। प्रमुख operators निम्नलिखित हैं:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment and Decrement Operators
- Bitwise Operators
- Conditional Operator
- Special Operators
| Type | Operators | मुख्य उपयोग |
|---|---|---|
| Arithmetic | + - * / % |
Mathematical calculations |
| Relational | == != > < >= <= |
Values की comparison |
| Logical | && || ! |
Logical conditions |
| Assignment | = += -= *= /= %= |
Value assign करना |
| Increment / Decrement | ++ -- |
Value को 1 से बढ़ाना या घटाना |
| Bitwise | & | ^ ~ << >> |
Bits पर operations |
| Conditional | ? : |
Short conditional expression |
| Special | sizeof, &, *, :: |
विशेष operations |
1. Arithmetic Operators
Arithmetic operators का उपयोग mathematical calculations (गणितीय गणनाओं) के लिए किया जाता है।
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | a + b |
- |
Subtraction | a - b |
* |
Multiplication | a * b |
/ |
Division | a / b |
% |
Modulus / Remainder | a % b |
Addition (+)
+ operator का उपयोग दो values को जोड़ने के लिए किया जाता है।
int a = 20;
int b = 10;
cout << a + b;
30
Subtraction (-)
- operator का उपयोग एक value को दूसरी value से घटाने के लिए किया जाता है।
int a = 20;
int b = 10;
cout << a - b;
10
Multiplication (*)
* operator का उपयोग multiplication के लिए किया जाता है।
int a = 20;
int b = 5;
cout << a * b;
100
Division (/)
/ operator का उपयोग division के लिए किया जाता है। यदि दोनों operands integers हैं, तो integer division में fractional part discard हो जाता है।
#include <iostream>
using namespace std;
int main()
{
int a = 17;
int b = 5;
cout << a / b;
return 0;
}
3
यदि decimal result चाहिए, तो operands में से कम-से-कम एक floating-point value होना चाहिए।
#include <iostream>
using namespace std;
int main()
{
double a = 17;
double b = 5;
cout << a / b;
return 0;
}
3.4
Modulus (%)
% operator division के बाद remainder (शेषफल) प्राप्त करने के लिए उपयोग किया जाता है।
#include <iostream>
using namespace std;
int main()
{
int a = 17;
int b = 5;
cout << a % b;
return 0;
}
2
17 / 5 = 3 और 17 % 5 = 2।
2. Relational Operators
Relational operators का उपयोग दो values या expressions के बीच comparison (तुलना) करने के लिए किया जाता है। इनका result सामान्यतः Boolean value true या false होता है।
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
Example of Relational Operators
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
cout << (a < b);
return 0;
}
1
यहाँ 10 < 20 true है, इसलिए output 1 प्राप्त होता है। Default output formatting में true को 1 और false को 0 के रूप में display किया जा सकता है।
3. Logical Operators
Logical operators का उपयोग एक या अधिक conditions को combine करने के लिए किया जाता है।
| Operator | Name | Meaning |
|---|---|---|
&& |
Logical AND | दोनों conditions true होनी चाहिए। |
|| |
Logical OR | कम-से-कम एक condition true होनी चाहिए। |
! |
Logical NOT | Boolean result को उलट देता है। |
Logical AND (&&)
&& operator का result true तभी होता है जब दोनों conditions true हों।
#include <iostream>
using namespace std;
int main()
{
int age = 20;
cout << (age >= 18 && age <= 60);
return 0;
}
1
Logical OR (||)
|| operator का result true होता है यदि कम-से-कम एक condition true हो।
#include <iostream>
using namespace std;
int main()
{
int marks = 35;
cout << (marks < 33 || marks > 80);
return 0;
}
0
यहाँ दोनों conditions false हैं, इसलिए result 0 है।
Logical NOT (!)
! operator Boolean expression के result को reverse करता है।
#include <iostream>
using namespace std;
int main()
{
bool passed = true;
cout << !passed;
return 0;
}
0
4. Assignment Operators
Assignment operators का उपयोग variable में value assign करने के लिए किया जाता है।
| Operator | Example | Equivalent Form |
|---|---|---|
= |
a = 10 |
Value assign करना |
+= |
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 |
Example of Assignment Operators
#include <iostream>
using namespace std;
int main()
{
int a = 10;
a += 5;
cout << a;
return 0;
}
15
5. Increment and Decrement Operators
Increment operator (++) किसी variable की value को 1 से बढ़ाता है। Decrement operator (--) किसी variable की value को 1 से घटाता है।
| Operator | Meaning | Example |
|---|---|---|
++ |
Increment by 1 | a++ |
-- |
Decrement by 1 | a-- |
Post-Increment
Post-increment में variable की current value पहले expression में use होती है और उसके बाद value 1 से बढ़ती है।
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout << a++ << endl;
cout << a;
return 0;
}
10
11
Pre-Increment
Pre-increment में variable की value पहले 1 से बढ़ती है और फिर updated value expression में use होती है।
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout << ++a;
return 0;
}
11
Post-Decrement
int a = 10;
cout << a-- << endl;
cout << a;
10
9
Pre-Decrement
int a = 10;
cout << --a;
9
6. Bitwise Operators
Bitwise operators integer values के individual bits पर operations perform करते हैं।
| Operator | Name |
|---|---|
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Left Shift |
>> |
Right Shift |
Bitwise AND (&)
Bitwise AND में प्रत्येक corresponding bit पर AND operation किया जाता है।
#include <iostream>
using namespace std;
int main()
{
int a = 5;
int b = 3;
cout << (a & b);
return 0;
}
1
Binary रूप में:
3 = 0011
5 & 3 = 0001 = 1
Bitwise OR (|)
Bitwise OR में यदि किसी corresponding position पर कम-से-कम एक bit 1 है, तो result bit 1 होती है।
int a = 5;
int b = 3;
cout << (a | b);
7
Bitwise XOR (^)
Bitwise XOR में दोनों corresponding bits अलग होने पर result 1 और समान होने पर 0 होता है।
int a = 5;
int b = 3;
cout << (a ^ b);
6
7. Conditional Operator
Conditional operator को ternary operator भी कहा जाता है क्योंकि इसमें तीन operands होते हैं। इसका syntax है:
condition ? expression1 : expression2;
यदि condition true होती है तो expression1 execute होता है, अन्यथा expression2 execute होता है।
#include <iostream>
using namespace std;
int main()
{
int marks = 75;
string result = (marks >= 33) ? "Pass" : "Fail";
cout << result;
return 0;
}
Pass
condition ? true-expression : false-expression होता है।
8. Special Operators
C++ में कुछ operators विशेष कार्यों के लिए उपयोग किए जाते हैं।
| Operator | Purpose |
|---|---|
sizeof |
Data type या object का size पता करना |
& |
Address-of operator के रूप में memory address प्राप्त करना |
* |
Pointer declaration और dereferencing में उपयोग |
:: |
Scope resolution |
sizeof Operator
#include <iostream>
using namespace std;
int main()
{
int number;
cout << sizeof(number);
return 0;
}
4
यहाँ output सामान्य 32-bit int implementation का उदाहरण है। Exact size platform पर निर्भर कर सकता है।
Address-of Operator (&)
& operator का उपयोग किसी variable का memory address प्राप्त करने के लिए किया जा सकता है।
int number = 10;
cout << &number;
इसका output एक memory address होगा, जो प्रत्येक execution में अलग हो सकता है।
Scope Resolution Operator (::)
:: scope resolution operator का उपयोग किसी identifier के scope को specify करने के लिए किया जाता है।
#include <iostream>
int value = 100;
int main()
{
int value = 50;
std::cout << value << std::endl;
std::cout << ::value;
return 0;
}
50
100
यहाँ value local variable को और ::value global variable को refer करता है।
Unary, Binary and Ternary Operators
Operands की संख्या के आधार पर operators को unary, binary और ternary operators में भी classify किया जा सकता है।
| Type | Operands | Example |
|---|---|---|
| Unary | One operand | ++a, !a |
| Binary | Two operands | a + b, a > b |
| Ternary | Three operands | a > b ? a : b |
Operator Precedence
जब एक expression में एक से अधिक operators होते हैं, तो C++ कुछ निर्धारित rules के अनुसार operators को evaluate करता है। इसे operator precedence कहा जाता है।
#include <iostream>
using namespace std;
int main()
{
int result = 10 + 5 * 2;
cout << result;
return 0;
}
20
यहाँ multiplication (*) की precedence addition (+) से अधिक है। इसलिए पहले 5 * 2 होगा और फिर 10 + 10।
() का उपयोग करना अच्छा practice है।
Example Using Multiple Operators
#include <iostream>
using namespace std;
int main()
{
int a = 20;
int b = 10;
int sum = a + b;
int difference = a - b;
int product = a * b;
int remainder = a % b;
cout << "Sum = " << sum << endl;
cout << "Difference = " << difference << endl;
cout << "Product = " << product << endl;
cout << "Remainder = " << remainder;
return 0;
}
Sum = 30
Difference = 10
Product = 200
Remainder = 0
Important Points
- Operators का उपयोग values और variables पर operations करने के लिए किया जाता है।
- Operator जिस value या variable पर कार्य करता है उसे operand कहा जाता है।
- Arithmetic operators mathematical calculations के लिए उपयोग होते हैं।
- Relational operators comparison के लिए उपयोग होते हैं।
- Logical operators conditions को combine करने के लिए उपयोग होते हैं।
- Assignment operators variables में values assign करने के लिए उपयोग होते हैं।
++और--क्रमशः value को 1 से बढ़ाते और घटाते हैं।- Bitwise operators individual bits पर operations perform करते हैं।
? :को conditional या ternary operator कहा जाता है।sizeofoperator memory size पता करने के लिए उपयोग किया जाता है।::scope resolution operator है।- Operators को operands की संख्या के आधार पर unary, binary और ternary भी classify किया जा सकता है।
Board Focus
Arithmetic →
+ - * / %Relational →
== != > < >= <=Logical →
&& || !Assignment →
= += -= *= /= %=Increment / Decrement →
++ --Bitwise →
& | ^ ~ << >>Conditional →
? :Special →
sizeof, &, *, ::
Board Important Questions
Very Short Answer Questions
Q1. Operator क्या है?
Answer: Operator एक symbol या keyword है जिसका उपयोग variables और values पर कोई operation करने के लिए किया जाता है।
Q2. Operand क्या है?
Answer: जिस value या variable पर operator कार्य करता है उसे operand कहा जाता है।
Q3. Arithmetic operators के नाम लिखिए।
Answer: +, -, *, / और % arithmetic operators हैं।
Q4. Modulus operator का क्या उपयोग है?
Answer: Modulus operator (%) division के बाद remainder प्राप्त करने के लिए उपयोग किया जाता है।
Q5. C++ में logical operators कौन-कौन से हैं?
Answer: &&, || और ! logical operators हैं।
Q6. Conditional operator कौन-सा है?
Answer: ? : conditional या ternary operator है।
Q7. Increment और decrement operators कौन-कौन से हैं?
Answer: ++ increment और -- decrement operator है।
Short Answer Questions
Q8. Arithmetic और Relational operators में अंतर बताइए।
| Arithmetic Operators | Relational Operators |
|---|---|
| Mathematical calculations के लिए उपयोग होते हैं। | दो values की comparison के लिए उपयोग होते हैं। |
+ - * / % |
== != > < >= <= |
उदाहरण: a + b |
उदाहरण: a > b |
Q9. Pre-increment और Post-increment में क्या अंतर है?
| Pre-Increment | Post-Increment |
|---|---|
| पहले value increment होती है। | पहले current value use होती है। |
Example: ++a |
Example: a++ |
Q10. Logical AND और Logical OR को समझाइए।
Answer: Logical AND (&&) में सभी conditions true होने पर result true होता है। Logical OR (||) में कम-से-कम एक condition true होने पर result true होता है।
Q11. Assignment operators क्या हैं?
Answer: Assignment operators का उपयोग variable को value assign करने या उसकी existing value को किसी operation के आधार पर update करने के लिए किया जाता है। =, +=, -=, *=, /= और %= इसके उदाहरण हैं।
Long Answer Questions
Q12. C++ में विभिन्न प्रकार के operators को उदाहरण सहित समझाइए।
Answer: C++ में Arithmetic, Relational, Logical, Assignment, Increment/Decrement, Bitwise और Conditional सहित कई प्रकार के operators होते हैं। Arithmetic operators calculations, relational operators comparison, logical operators conditions, assignment operators values assign करने, increment/decrement operators values को 1 से बदलने, bitwise operators bits पर operations और conditional operator short conditional expression के लिए उपयोग होता है।
Q13. निम्न program में विभिन्न operators का उपयोग समझाइए।
#include <iostream>
using namespace std;
int main()
{
int a = 20;
int b = 10;
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;
cout << "Greater = " << (a > b);
return 0;
}
Addition = 30
Subtraction = 10
Multiplication = 200
Division = 2
Remainder = 0
Greater = 1
Quick Revision
- Arithmetic:
+ - * / % - Relational:
== != > < >= <= - Logical:
&& || ! - Assignment:
= += -= *= /= %= - Increment:
++ - Decrement:
-- - Bitwise:
& | ^ ~ << >> - Conditional:
? : - Special:
sizeof,&,*,::
Practice Questions
- Operator और Operand क्या हैं?
- C++ के विभिन्न operators के नाम लिखिए।
- Arithmetic operators को उदाहरण सहित समझाइए।
- Relational operators क्या हैं?
- Logical AND और Logical OR में क्या अंतर है?
- Assignment operators को उदाहरण सहित समझाइए।
- Pre-increment और Post-increment में अंतर बताइए।
- Bitwise operators क्या हैं? इनके नाम लिखिए।
- Conditional operator का syntax लिखिए और उदाहरण दीजिए।
- निम्न expression का output बताइए:
int a = 17; int b = 5; cout << a / b << endl; cout << a % b; - निम्न expression का output बताइए:
int a = 10; cout << a++ << endl; cout << a; - एक C++ program लिखिए जिसमें दो numbers लेकर उनका addition, subtraction, multiplication, division और remainder प्रदर्शित किया जाए।