सी ++ ऑपरेटर्स

इस ट्यूटोरियल में, हम उदाहरणों की मदद से C ++ में विभिन्न प्रकार के ऑपरेटरों के बारे में जानेंगे। प्रोग्रामिंग में, एक ऑपरेटर एक प्रतीक है जो एक मूल्य या एक चर पर संचालित होता है।

ऑपरेटर प्रतीक हैं जो चर और मूल्यों पर संचालन करते हैं। उदाहरण के लिए, +इसके अलावा के लिए उपयोग किया जाने वाला ऑपरेटर है, जबकि -घटाव के लिए उपयोग किया जाने वाला ऑपरेटर है।

C ++ में ऑपरेटरों को 6 प्रकारों में वर्गीकृत किया जा सकता है:

  1. अंकगणितीय आपरेटर
  2. असाइनमेंट ऑपरेटर्स
  3. संबंधपरक संकारक
  4. लॉजिकल ऑपरेटर्स
  5. बिटवाइज ऑपरेटर्स
  6. अन्य संचालक

1. सी ++ अंकगणितीय ऑपरेटर

अंकगणित ऑपरेटरों का उपयोग चर और डेटा पर अंकगणितीय संचालन करने के लिए किया जाता है। उदाहरण के लिए,

 a + b; 

यहां, +ऑपरेटर का उपयोग दो चर a और b को जोड़ने के लिए किया जाता है। इसी तरह C ++ में विभिन्न अन्य अंकगणितीय ऑपरेटर हैं।

ऑपरेटर ऑपरेशन
+ जोड़
- घटाव
* गुणन
/ विभाजन
% मोडुलो ऑपरेशन (विभाजन के बाद शेष)

उदाहरण 1: अंकगणित संचालक

  #include using namespace std; int main() ( int a, b; a = 7; b = 2; // printing the sum of a and b cout << "a + b = " << (a + b) << endl; // printing the difference of a and b cout << "a - b = " << (a - b) << endl; // printing the product of a and b cout << "a * b = " << (a * b) << endl; // printing the division of a by b cout << "a / b = " << (a / b) << endl; // printing the modulo of a by b cout << "a % b = " << (a % b) << endl; return 0; ) 

आउटपुट

 a + b = 9 a - b = 5 a * b = 14 a / b = 3 a% b = 1 

इधर, ऑपरेटरों +, -और *गणना इसके अलावा, घटाव, गुणा और क्रमश: हम उम्मीद हो सकता है के रूप में।

/ प्रभाग संचालक

(a / b)हमारे कार्यक्रम में ऑपरेशन पर ध्यान दें । /ऑपरेटर विभाजन ऑपरेटर है।

जैसा कि हम ऊपर के उदाहरण से देख सकते हैं, यदि एक पूर्णांक दूसरे पूर्णांक से विभाजित होता है, तो हम भागफल प्राप्त करेंगे। हालांकि, यदि कोई भी भाजक या लाभांश एक फ्लोटिंग-पॉइंट नंबर है, तो हमें परिणाम दशमलव में मिलेगा।

 C ++ में, 7/2 3 7.0 / 2 3.5 3.5 / 2.0 3.5 7.0 / 2.0 3.5 है 

% मोडुलो ऑपरेटर

मॉडुलो ऑपरेटर %शेष की गणना करता है। जब a = 9विभाजित किया जाता है b = 4, तो शेष 1 होता है

नोट:% ऑपरेटर केवल पूर्णांकों के साथ प्रयोग किया जा सकता है।

वेतन वृद्धि और कमी ऑपरेटर

C ++ वेतन वृद्धि और वेतन वृद्धि ऑपरेटर भी प्रदान करता है: ++और --क्रमशः। ++द्वारा संकार्य का मूल्य बढ़ जाता 1 , जबकि --यह तक घट जाता है 1

उदाहरण के लिए,

 int num = 5; // increasing num by 1 ++num; 

यहां, संख्या का मूल्य 5 के अपने प्रारंभिक मूल्य से बढ़कर 6 हो जाता है ।

उदाहरण 2: वेतन वृद्धि और कमी ऑपरेटर

 // Working of increment and decrement operators #include using namespace std; int main() ( int a = 10, b = 100, result_a, result_b; // incrementing a by 1 and storing the result in result_a result_a = ++a; cout << "result_a = " << result_a << endl; // decrementing b by 1 and storing the result in result_b result_b = --b; cout << "result_b = " << result_b << endl; return 0; ) 

आउटपुट

 result_a = 11 result_b = 99 

उपरोक्त कार्यक्रम में, हमने उपसर्गों के रूप में उपयोग किया ++और --ऑपरेटर किया । हम इन ऑपरेटरों को पोस्टफिक्स के रूप में भी इस्तेमाल कर सकते हैं ।

जब इन ऑपरेटरों को उपसर्ग के रूप में उपयोग किया जाता है तो थोड़ा अंतर होता है जब उनका उपयोग उपसर्ग के रूप में किया जाता है।

इन ऑपरेटरों के बारे में अधिक जानने के लिए वेतन वृद्धि और वेतन वृद्धि ऑपरेटरों पर जाएँ।

2. सी ++ असाइनमेंट ऑपरेटर्स

C ++ में, असाइनमेंट ऑपरेटर्स का उपयोग वैरिएबल को मान असाइन करने के लिए किया जाता है। उदाहरण के लिए,

 // assign 5 to a a = 5; 

यहाँ, हमने 5वेरिएबल a का मान दिया है।

ऑपरेटर उदाहरण के बराबर
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;

उदाहरण 2: असाइनमेंट ऑपरेटर्स

 #include using namespace std; int main() ( int a, b, temp; // 2 is assigned to a a = 2; // 7 is assigned to b b = 7; // value of a is assigned to temp temp = a; // temp will be 2 cout << "temp = " << temp << endl; // assigning the sum of a and b to a a += b; // a = a +b cout << "a = " << a << endl; return 0; ) 

आउटपुट

 अस्थायी = 2 ए = 9 

3. सी ++ रिलेशनल ऑपरेटर्स

A relational operator is used to check the relationship between two operands. For example,

 // checks if a is greater than b a> b; 

Here, > is a relational operator. It checks if a is greater than b or not.

If the relation is true, it returns 1 whereas if the relation is false, it returns 0.

Operator Meaning Example
== Is Equal To 3 == 5 gives us false
!= Not Equal To 3 != 5 gives us true
> Greater Than 3> 5 gives us false
< Less Than 3 < 5 gives us true
>= Greater Than or Equal To 3>= 5 give us false
<= Less Than or Equal To 3 <= 5 gives us true

Example 4: Relational Operators

 #include using namespace std; int main() ( int a, b; a = 3; b = 5; bool result; result = (a == b); // false cout << "3 == 5 is " << result << endl; result = (a != b); // true cout << "3 != 5 is " << result < b; // false cout < 5 is " << result << endl; result = a < b; // true cout << "3 < 5 is " << result <= b; // false cout <= 5 is " << result << endl; result = a <= b; // true cout << "3 <= 5 is " << result << endl; return 0; ) 

Output

 3 == 5 is 0 3 != 5 is 1 3> 5 is 0 3 = 5 is 0 3 <= 5 is 1 

Note: Relational operators are used in decision making and loops.

4. C++ Logical Operators

Logical operators are used to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.

Operator Example Meaning
&& expression1 && expression2 Logical AND.
True only if all the operands are true.
|| expression1 || expression2 Logical OR.
True if at least one of the operands is true.
! !expression Logical NOT.
True only if the operand is false.

In C++, logical operators are commonly used in decision making. To further understand the logical operators, let's see the following examples,

 Suppose, a = 5 b = 8 Then, (a> 3) && (b> 5) evaluates to true (a> 3) && (b 3) || (b> 5) evaluates to true (a> 3) || (b < 5) evaluates to true (a < 3) || (b 3) evaluates to false 

Example 5: Logical Operators

 #include using namespace std; int main() ( bool result; result = (3 != 5) && (3 < 5); // true cout << "(3 != 5) && (3 < 5) is " << result << endl; result = (3 == 5) && (3 < 5); // false cout << "(3 == 5) && (3 < 5) is " << result < 5); // false cout < 5) is " << result << endl; result = (3 != 5) || (3 < 5); // true cout << "(3 != 5) || (3 < 5) is " << result < 5); // true cout < 5) is " << result < 5); // false cout < 5) is " << result << endl; result = !(5 == 2); // true cout << "!(5 == 2) is " << result << endl; result = !(5 == 5); // false cout << "!(5 == 5) is " << result << endl; return 0; ) 

Output

 (3 != 5) && (3 < 5) is 1 (3 == 5) && (3 5) is 0 (3 != 5) || (3 5) is 1 (3 == 5) || (3 < 5) is 0 !(5 == 2) is 1 !(5 == 5) is 0 

Explanation of logical operator program

  • (3 != 5) && (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
  • (3 == 5) && (3 < 5) evaluates to 0 because the operand (3 == 5) is 0 (false).
  • (3 == 5) && (3> 5) evaluates to 0 because both operands (3 == 5) and (3> 5) are 0 (false).
  • (3 != 5) || (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
  • (3 != 5) || (3> 5) evaluates to 1 because the operand (3 != 5) is 1 (true).
  • (3 == 5) || (3> 5) evaluates to 0 because both operands (3 == 5) and (3> 5) are 0 (false).
  • !(5 == 2) evaluates to 1 because the operand (5 == 2) is 0 (false).
  • !(5 == 5)करने के लिए मूल्यांकन करता है 0 क्योंकि संकार्य (5 == 5)है 1 (सही)।

5. सी ++ बिटवाइज ऑपरेटर्स

C ++ में, बिटकॉइन ऑपरेटरों का उपयोग व्यक्तिगत बिट्स पर संचालन करने के लिए किया जाता है। उनका उपयोग केवल एक साथ charऔर intडेटा प्रकार के साथ किया जा सकता है ।

ऑपरेटर विवरण
& बाइनरी और
| बाइनरी या
^ बाइनरी एक्सओआर
~ बाइनरी वन का पूरक
<< बाइनरी शिफ्ट लेफ्ट
>> बाइनरी शिफ्ट राइट

अधिक जानने के लिए, C ++ बिटवाइज़ ऑपरेटर्स पर जाएँ।

इसके अलावा ऑपरेटरों ऊपर चर्चा से, वहाँ इस तरह के रूप में कुछ अन्य ऑपरेटरों, कर रहे हैं sizeof, ?, ., &, आदि, कि बड़े करीने से एक या एक और प्रकार में वर्गीकृत नहीं किया जा सकता। हम बाद के ट्यूटोरियल में इन ऑपरेटरों के बारे में अधिक जानेंगे।

दिलचस्प लेख...