Operators mathematical symbols (चिह्न) है जो की गणितीय (mathematical) और तार्किक (logical) calculation करने के उपयोग किये जाते, उदाहरण के लिए +, -, *, / आदि operators है।
Example
#include<stdio.h> int main() { int a = 10; int b = 5; int c = a + b; // + is a operator and a, b are operand return 0; }
उदाहरण में + एक operator है जो addition कर रहा है एवं a ,b operands है जिन पर operation perform (variable में store values पर ) होना है।
Operators के प्रकार (Types of operators):-
- Arithmetic Operators
- Increment and Decrement Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Conditional Operator
Arithmetic Operators
Arithmetic operators का उपयोग basic maths को हल करने के लिए किया जाता है जैसे + (जोड़ना), – (घटना), * (गुणा), / (भाग), एवं % (शेषफल)
Operator | Operator Name | Description |
---|---|---|
+ | Addition (जोड़ना) | values को जोड़ना |
– | Subtraction (घटना) | values को घटाना |
* | Multiplication (गुणा) | values को गुणा करना |
/ | Division (भाग) | values का भाग करना |
% | Modulo (शेषफल) | values का शेषफल निकलना |
#include<stdio.h> int main() { int a = 10; int b = 5; printf("Addition = %d\n", a+b); printf("Subtraction = %d\n", a-b); printf("Multiplication = %d\n", a*b); printf("Division = %d\n", a/b); printf("Modulo = %d\n", a%b); return 0; } // OUTPUT /* Addition = 15 Subtraction = 5 Multiplication = 50 Division = 2 Modulo = 0 */
Increment/Decrement
Increment and decrement operators को unary operators भी कहते है, जिन्हे apply करने लिए केवल एक operand की आवश्यकता होती है। इन operators का उपयोग variable में stored को increment(बढ़ाना) या decrement(घाटना) करने के लिए उपयोग करते है।
- increment/decrement operator को unary operators भी कहते है |
Types of unary operators
Operator | Operator Name | Description |
---|---|---|
++ | increment operator | value को +1 बढ़ाता (increment) |
— | decrement operator | value को -1 करता है (decrement) |
Types of increment operators
pre-increment – value को पहले increment(बढ़ाना) करो फिर use करो।
post-increment – value को पहले use करो फिर increment(बढ़ाना) करो।
#include <stdio.h> int main(int argc, char const *argv[]) { int a = 5; int b = a++; // in post increment first use than increment printf("Value of b: %d\\n", b); b = ++a; // in pre increment first increment than use printf("Value of b: %d\\n", b); return 0; } // Output // Value of b: 5 // Value of b: 7
Types of decrement operators
pre-decrement – value को पहले decrement(घटाना) करो फिर use करो।
post-decrement – value को पहले use करो फिर decrement(घटाना) करो।
#include <stdio.h> int main(int argc, char const *argv[]) { int a = 5; int b = a--; // in post decrement first use than increment printf("Value of b: %d\n", b); b = --a; // in post decrement first decrement and than use printf("Value of b: %d\n", b); return 0; } // Output // Value of b: 5 // Value of b: 3
Relational Operators
Relational operators का उपयोग value या मात्रा को compare करने के लिए किया जाता है, relational operators को comparison operators भी कहते है।
Operator | Operator Name | Description |
---|---|---|
== | double equal | यदि left एवं right की value बराबर है तो 1 आयेगा नहीं तो 0 आयेगा। |
!= | not equal | यदि left एवं right की value बराबर नहीं है तो 1 आयेगा नहीं तो 0 आयेगा। |
> | less than | यदि left की value, right की value से बड़ी है तो 1 आएगा नहीं तो 0 आएगा। |
< | greater than | यदि left की value, right की value से छोटी है तो 1 आएगा नहीं तो 0 आएगा। |
=> | greater than और equal | यदि left की value, right की value से बड़ी है या बराबर है तो 1 आएगा नहीं तो 0 आएगा। |
=< | less than और equal | यदि left की value, right की value से छोटी है या बराबर है तो 1 आएगा नहीं तो 0 आएगा। |
#include <stdio.h> int main(int argc, char const *argv[]) { int a = 5; int b = 10; // comparison operators // == => double equal printf("== a and b are equal? => %d\n", a==b); // != not equal printf("!= a and b are not equal? => %d\n", a!=b); // < less than printf("a < b is a less than b => %d\n", a < b); // > greater than printf("a < b is a greater than b => %d\n", a > b); // <= less than or equal printf("a <= b is a less than or equal to b => %d\n", a <= b); // >= greater than or equal printf("a >= b a is greater than or equal to b => %d\n", a >= b); return 0; } // ---Output--- /* == a and b are equal? => 0 != a and b are not equal? => 1 a < b is a less than b => 1 a < b is a greater than b => 0 a <= b is a less than or equal to b => 1 a >= b a is greater than or equal to b => 0 */
Logical Operators
जब एक से अधिक conditions check करनी पड़नी है तब वहां logical operators कहते है, Logical operators इस प्रकार है : &&
(meaning logical AND), ||
(meaning logical OR) एवं !
(meaning logical NOT).
Operator | Operator Name | Description |
---|---|---|
&& | Logical AND | Result 1 यदि दोनों conditions true है, नहीं तो 0 |
|| | Logical OR | Result 1 यदि दोनों में से एक conditions true है या दोनों conditions true है, नहीं तो 0 |
! | Logical NOT | Result 1 यदि condition false है, Reverse the result. |
#include <stdio.h> int main(int argc, char const *argv[]) { int a=20, b=10, c=15; // logical AND (&&) //return 1 if a is greater than b and a is greater than c printf("%d\n", a > b && a > c); return 0; }
#include <stdio.h> int main(int argc, char const *argv[]) { int a=6, b=10, c=5; // logical OR (||) //return 1 if a is greater than b or a is greater than c printf("%d\n", a > b || a > c); return 0; }
#include <stdio.h> int main(int argc, char const *argv[]) { int a=1; // logical OR (||) // return 0 if a is non-zero else 0 printf("%d\n", !a); return 0; }
Bitwise Operators
Bitwise operators का उपयोग संख्याओं पर bit (0,1) level पर operation करने के लिए करते है, जैसे bit को सेट करने के लिए set bit, bit को प्राप्त करने की लिए get bit आदि operations.
Operator | Operator Name | Description |
---|---|---|
& | Bitwise AND | Result 0, यदि एक भी bit 0 है तो। |
| | Bitwise OR | Result 1 , यदि एक भी bit 1 है तो। |
~ | 1’s Complement | Number का complement |
^ | XOR Operator | सामान bit पर result 0, असमान bit पर result 1 |
>> | Right Shift | bits shift होगी left से right की ओर |
<< | Left Shift | bits shift होगी right से left की ओर |
Example of Bitwise Operators
#include<stdio.h> int main() { int a=2; int b=4; printf("a&b = %d\n", a&b); printf("a|b = %d\n", a|b); printf("a~b = %d\n", ~b); // -(n+1) printf("a~b = %d\n", ~a); printf("a^b = %d\n", a^b); printf("b>>a = %d\n", b>>a); // b/2^a printf("b<<a = %d\n", b<<a); // b*2^a return 0; } // OUTPUT // a&b = 0 // a|b = 6 // a~b = -5 // a~b = -3 // a^b = 6 // b>>a = 1 // b<<a = 16
Assignment Operators
Assignment operators का उपयोग value को variable assign करता है या variable को initialize करता है।
Operator | Operator Name | Description |
---|---|---|
= | Equal | a = 10 यहाँ = value assign कर रहा है variable a में। |
+= | Plus equal | a += 5 variable जो भी value है उसमे 5 add करके, a में store करना (a = a+5) |
-= | Minus equal | a -= 5 variable जो भी value है उसमे 5 minus करके, a में store करना (a = a-5) |
*= | Multiply equal | a *= 5 variable जो भी value है उसमे 5 multiply करके, a में store करना (a = a*5) |
/= | Divide equal | a/= 5 variable जो भी value hai उसमे 5 divide करके, a में store करना (a = a/5) |
%= | Modulo equal | a %= 5 variable जो भी value hai उसमे 5 modulo करके, a में store करना (a = a%5) |
Conditional Operators
C offers a ternary operator, which is the conditional operator (?:
in combination), to construct conditional expressions.
Special Operators
C supports some special operators
Operator | Description |
---|---|
sizeof() | Returns the size of a memory location. |
& | Returns the address of a memory location. |
* | Pointer to a variable. |