ऑपरेटरों में सी

इस ट्यूटोरियल में, आप C प्रोग्रामिंग में विभिन्न ऑपरेटरों के बारे में उदाहरणों की मदद से सीखेंगे।

एक ऑपरेटर एक प्रतीक है जो एक मूल्य या एक चर पर संचालित होता है। उदाहरण के लिए: + एक ऑपरेटर है जो अतिरिक्त प्रदर्शन करता है।

C के पास विभिन्न ऑपरेशन करने के लिए ऑपरेटरों की एक विस्तृत श्रृंखला है।

C अंकगणित संचालक

एक अंकगणितीय ऑपरेटर संख्यात्मक मानों (स्थिरांक और चर) पर गणितीय संचालन जैसे जोड़, घटाव, गुणा, भाग आदि करता है।

ऑपरेटर संचालक का अर्थ
+ जोड़ या अपरिपक्व प्लस
- घटाव या एकात्मक शून्य
* गुणन
/ विभाजन
% विभाजन के बाद शेष (मॉडुलो डिवीजन)

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

 // Working of arithmetic operators #include int main() ( int a = 9,b = 4, c; c = a+b; printf("a+b = %d ",c); c = a-b; printf("a-b = %d ",c); c = a*b; printf("a*b = %d ",c); c = a/b; printf("a/b = %d ",c); c = a%b; printf("Remainder when a divided by b = %d ",c); return 0; )

आउटपुट

 a + b = 13 ab = 5 a * b = 36 a / b = 2 शेष जब b = 1 से भाग दिया जाए

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

सामान्य गणना में, 9/4 = 2.25। हालाँकि, आउटपुट 2प्रोग्राम में है।

ऐसा इसलिए है क्योंकि दोनों चर ए और बी पूर्णांक हैं। इसलिए, आउटपुट भी पूर्णांक है। कंपाइलर दशमलव बिंदु के बाद शब्द की उपेक्षा करता है और 2इसके बजाय उत्तर दिखाता है 2.25

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

मान लीजिए a = 5.0, b = 2.0, c = 5और d = 2। फिर C प्रोग्रामिंग में,

 // दोनों में से एक ऑपरेंड एक फ्लोटिंग-पॉइंट नंबर a / b = 2.5 a / d = 2.5 c / b = 2.5 // दोनों ऑपरेंड पूर्णांक c / d = 2 हैं

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

C प्रोग्रामिंग में 1 से एक ऑपरेंड (स्थिर या चर) के मान को बदलने के लिए दो ऑपरेटर वेतन वृद्धि ++और गिरावट --है।

वृद्धि ++1 से मूल्य बढ़ाती है जबकि वेतन वृद्धि 1 --से मूल्य कम हो जाती है। ये दो ऑपरेटर एकात्मक ऑपरेटर हैं, जिसका अर्थ है कि वे केवल एक ही ऑपरेंड पर काम करते हैं।

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

 // Working of increment and decrement operators #include int main() ( int a = 10, b = 100; float c = 10.5, d = 100.5; printf("++a = %d ", ++a); printf("--b = %d ", --b); printf("++c = %f ", ++c); printf("--d = %f ", --d); return 0; )

आउटपुट

 ++ a = 11 --b = 99 ++ c = 11.500000 --d = 99.500000

यहां, ऑपरेटरों ++और --उपसर्गों के रूप में उपयोग किया जाता है। इन दो ऑपरेटरों को भी उपसर्गों की तरह इस्तेमाल किया जा सकता है a++और a--। जब पोस्टफ़िक्स के रूप में उपयोग किया जाता है तो वेतन वृद्धि और क्षरण ऑपरेटर कैसे काम करते हैं, इस बारे में अधिक जानने के लिए इस पृष्ठ पर जाएँ।

C असाइनमेंट ऑपरेटर

एक असाइनमेंट ऑपरेटर का उपयोग किसी वैरिएबल के मान को असाइन करने के लिए किया जाता है। सबसे आम असाइनमेंट ऑपरेटर है=

ऑपरेटर उदाहरण के समान
= = ए = बी ए = बी
+ = ए + = बी a = a + b
- = ए - = बी a = ab
* = ए * = बी a = a * b
/ = ए / = बी a = a / b
% = अ% = b a = a% b

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

 // Working of assignment operators #include int main() ( int a = 5, c; c = a; // c is 5 printf("c = %d", c); c += a; // c is 10 printf("c = %d", c); c -= a; // c is 5 printf("c = %d", c); c *= a; // c is 25 printf("c = %d", c); c /= a; // c is 5 printf("c = %d", c); c %= a; // c = 0 printf("c = %d", c); return 0; )

आउटपुट

 c = 5 c = 10 c = 5 c = 25 c = 5 c = 0

C रिलेशनल ऑपरेटर्स

एक रिलेशनल ऑपरेटर दो ऑपरेंड के बीच संबंधों की जाँच करता है। यदि संबंध सत्य है, तो यह 1 लौटता है; यदि संबंध गलत है, तो यह मान 0 देता है।

रिलेशनल ऑपरेटर का उपयोग निर्णय लेने और लूप में किया जाता है।

ऑपरेटर संचालक का अर्थ उदाहरण
== के बराबर 5 == 3 0 पर मूल्यांकन किया है
> Greater than 5> 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5>= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0

Example 4: Relational Operators

 // Working of relational operators #include int main() ( int a = 5, b = 5, c = 10; printf("%d == %d is %d ", a, b, a == b); printf("%d == %d is %d ", a, c, a == c); printf("%d> %d is %d ", a, b, a> b); printf("%d> %d is %d ", a, c, a> c); printf("%d < %d is %d ", a, b, a < b); printf("%d < %d is %d ", a, c, a = %d is %d ", a, b, a>= b); printf("%d>= %d is %d ", a, c, a>= c); printf("%d <= %d is %d ", a, b, a <= b); printf("%d <= %d is %d ", a, c, a <= c); return 0; )

Output

 5 == 5 is 1 5 == 10 is 0 5> 5 is 0 5> 10 is 0 5 < 5 is 0 5 = 5 is 1 5>= 10 is 0 5 <= 5 is 1 5 <= 10 is 1 

C Logical Operators

An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.

Operator Meaning Example
&& Logical AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0.
|| Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1.
! Logical NOT. True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0.

Example 5: Logical Operators

 // Working of logical operators #include int main() ( int a = 5, b = 5, c = 10, result; result = (a == b) && (c> b); printf("(a == b) && (c> b) is %d ", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d ", result); result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d ", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d ", result); result = !(a != b); printf("!(a != b) is %d ", result); result = !(a == b); printf("!(a == b) is %d ", result); return 0; ) 

Output

 (a == b) && (c> b) is 1 (a == b) && (c < b) is 0 (a == b) || (c < b) is 1 (a != b) || (c < b) is 0 !(a != b) is 1 !(a == b) is 0 

Explanation of logical operator program

  • (a == b) && (c> 5) evaluates to 1 because both operands (a == b) and (c> b) is 1 (true).
  • (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
  • (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
  • (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
  • !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
  • !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

C Bitwise Operators

During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.

Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right

Visit bitwise operator in C to learn more.

Other Operators

Comma Operator

Comma operators are used to link related expressions together. For example:

 int a, c = 5, d;

The sizeof operator

The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).

Example 6: sizeof Operator

 #include int main() ( int a; float b; double c; char d; printf("Size of int=%lu bytes",sizeof(a)); printf("Size of float=%lu bytes",sizeof(b)); printf("Size of double=%lu bytes",sizeof(c)); printf("Size of char=%lu byte",sizeof(d)); return 0; )

Output

 Size of int = 4 bytes Size of float = 4 bytes Size of double = 8 bytes Size of char = 1 byte 

अन्य ऑपरेटरों जैसे टर्नरी ऑपरेटर ?:, रेफरेंस ऑपरेटर &, डीरेफेरेंस ऑपरेटर *और सदस्य चयन ऑपरेटर के ->बारे में बाद के ट्यूटोरियल में चर्चा की जाएगी।

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