Operators in C
Types of Operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Increment / Decrement Operators
- Conditional / Ternary Operator
1. Arithmetic Operators
| Operators | Explaination |
|---|---|
| + (Addition) | ये दो Operands को add करता है | |
| - (Subtraction) | ये right operand से left operand को निकाल देता है | |
| * (Multiplication) | ये दो Operands को multiply करता है | |
| / (Division) | ये right operand द्वारा left operand को divide करता है | |
| % (Modulus) | ये right operand द्वारा left operand को divide करके remainder निकालता है | |
For eg.
Source Code :
#include <stdio.h>
int main() {
int a,b,c;
printf("Enter two numbers ");
scanf("%d%d",&a,&b);
c=a+b;
printf("Addition of a and b is %d\n",c);
c=a-b;
printf("Subtraction of a and b is %d\n",c);
c=a*b;
printf("Multiplication of a and b is %d\n",c);
c=a/b;
printf("Division of a and b is %d\n",c);
c=a%b;
printf("Remainder of a and b is %d\n",c);
return 0;
}
Output
Enter two numbers 5 4 Addition of a and b is 9 Subtraction of a and b is 1 Multiplication of a and b is 20 Division of a and b is 1 Remainder of a and b is 1
2. Relational Operators
| Operators | Explaination |
|---|---|
| < (less than) | एक Operand की value दूसरे Operand से कम हो तो ये true return करता है | for eg. num1=5; num2=6; num1 < num2 |
| > (greater than) | एक Operand की value दूसरे Operand से ज्यादा हो तो ये true return करता है | for eg. num1=6; num2=5; num1 > num2 |
| <= (less than or equal to) | एक Operand की value दूसरे Operand से कम हो या बराबर (equal) हो तो ये true return करता है | for eg. num1=5; num2=5; num1 <= num2 |
| >= (greater than or equal to) | एक Operand की value दूसरे Operand से ज्यादा हो या बराबर (equal) हो तो ये true return करता है | for eg. num1=5; num2=5; num1 >= num2 |
| == (equal to) | दो Operands जब बराबर(equal) होते है, तब ये true return करता है | |
| != (not equal to) | दो Operands जब एक-दूसरे से अलग होते है, तब ये true return करता है | |
For eg.
Source Code :
#include <stdio.h>
int main(){
int a=6, b=5;
if(a<b){
printf(" a is less than b\n");
}
else{
printf(" a is greater than b\n");
}
if(a<=b){
printf(" a is less than b\n");
}
else{
printf(" a is greater than b\n");
}
if(a>b){
printf(" a is greater than b\n");
}
else{
printf(" a is less than b\n");
}
if(a>=b){
printf(" a is greater than b\n");
}
else{
printf(" a is less than b\n");
}
if(a==b){
printf(" a is equal to b\n");
}
else{
printf(" a is not equal to b\n");
}
return 0;
}
Output
a is greater than b a is greater than b a is greater than b a is greater than b a is not equal to b
3. Logical Operators
| Operators | Explaination |
|---|---|
| && (logical AND) | अगर दोनों conditions true हो तो ये true return करेगा | for eg. (5<6) && (6>5) |
| || (logical OR) | अगर दोनों में से एक भी true है , तो ये true return करेगा | for eg. (5<6) || (6>5) |
| ! (logical NOT) | अगर condition true हो तो ये उसे false कर देता है | for eg. !((5<6) && (6>5)) !((5<6) || (6>5)) |
For eg.
Source Code :
#include <stdio.h>
int main(){
if((5<6) && (6>5)){
printf("Condition is true.\n");
}
else{
printf("Condition is false.\n");
}
if((5<6) && (6>5)){
printf("Condition is true.\n");
}
else{
printf("Condition is false.\n");
}
if(!((5<6) && (5>6))){
printf("Condition is true.\n");
}
else{
printf("Condition is false.\n");
}
return 0;
}
Output
Condition is true. Condition is true. Condition is true.
4. Bitwise Operators
Truth Table for &, |, ^| a | b | a & b | a | b | a ^ b |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
| Operation on AND(a&b) | ||
|---|---|---|
| Decimal Value | Binary Value | |
| अगर a = 20, b = 12 हो तो, | ||
| 20 | 0 0 0 1 0 1 0 0 | |
| 12 | 0 0 0 0 1 1 0 0 | |
| 4 | 0 0 0 0 0 1 0 0 | |
| Operation on OR(a|b) | ||
|---|---|---|
| Decimal Value | Binary Value | |
| अगर a = 20, b = 12 हो तो, | ||
| 20 | 0 0 0 1 0 1 0 0 | |
| 12 | 0 0 0 0 1 1 0 0 | |
| 28 | 0 0 0 1 1 1 0 0 | |
| Operation on XOR(a^b) | ||
|---|---|---|
| Decimal Value | Binary Value | |
| अगर a = 20, b = 12 हो तो, | ||
| 20 | 0 0 0 1 0 1 0 0 | |
| 12 | 0 0 0 0 1 1 0 0 | |
| 24 | 0 0 0 1 1 0 0 0 | |
Binary Left Shift( << ) and Right Shift( >> )
- Left Shift(<<) for e.g. a=20; /* 0001 0100 */ a << 2 में numeric value के binary value में हर binary number को 2 binary numbers left side से shift करता है | for e.g.a=20; /* 0001 0100 */ तो इसका 0101 0000 मतलब 80 हो जायेगा |
- Right Shift(>>) for e.g. a=20; /* 0001 0100 */ ये Left shift से बिलकुल उलट है | Right Shift a>> 2 में numeric value के binary value में हर binary number को 2 binary numbers right side से shift करता है | for e.g.a=20; /* 0001 0100 */ तो इसका 0000 0101 मतलब 5 हो जायेगा |
Complement Operator (~)
- ये Operator सारे bit reverse करता है |
- ये Operator 0 को 1 कर देता है और 1 को 0 कर देता है |
| Operation on Complement( ~ ) | ||
|---|---|---|
| Decimal Value | Binary Value | |
| ~12 | 0 0 0 0 1 1 0 0 | |
| 243 | 1 1 1 1 0 0 1 1 | |
यहाँ पर Output -13 आने के बजाय 243 आया ऐसा क्यों ?
2's Complement of 243 -(reverse of 243 in binary + 1)
| Operation on 2's Complement( ~ ) | ||
|---|---|---|
| Decimal Value | Binary Value | 2's Complement |
| 243 | 1111 0011 | -(0000 1100+1) = -(0000 1101) = -13(output) |
For eg.
Source Code :
#include <stdio.h>
int main() {
int a=20; /* 0001 0100 */
int b=12; /* 0000 1100 */
int c;
c=a&b;
printf("value of c is %d",c); /* 4 = 0000 0100 */
c=a|b;
printf("\nvalue of c is %d",c); /* 28 = 0001 1100 */
c=a^b;
printf("\nvalue of c is %d",c); /* 24 = 0001 1000 */
c=a<<2;
printf("\nvalue of c is %d",c); /* 80 = 0101 0000 */
c=a>>2;
printf("\nvalue of c is %d",c); /* 5 = 0000 0101 */
printf("\nvalue of b is %d",~b); /* -13 = 1111 0011 */
return 0;
}
Output
value of c is 4
value of c is 28
value of c is 24
value of c is 80
value of c is 5
value of b is -13
5. Assignment Operators
Assignment Operators ग्यारह प्रकार के होते है |
- Assignment Operator (=)
- Add Assignment Operator (+=)
- Subtract Assignment Operator (-=)
- Multiply Assignment Operator (*=)
- Divide Assignment Operator (/=)
- Modulus Assignment Operator (%=)
- Bitwise AND Assignment Operator (&=)
- Bitwise OR Assignment Operator (|=)
- Bitwise XOR Assignment Operator (^=)
- Left Shift Assignment Operator (<<=)
- Right Shift Assignment Operator (>>=)
| Operators | Examples |
|---|---|
| = (assignment) | c = a + b |
| += (add assignment) | c += a same as c = c + a |
| -= (subtract assignment) | c -= a same as c = c - a |
| *= (multiply assignment) | c *= a same as c = c * a |
| /= (divide assignment) | c /= a same as c = c / a |
| %= (modulus assignment) | c %= a same as c = c % a |
| &= (AND assignment) | c &= a same as c = c & a |
| |= (OR assignment) | c |= a same as c = c | a |
| ^= (XOR assignment) | c ^= a same as c = c ^ a |
| <<= (Left Shift assignment) | c <<= a same as c = c << a |
| >>= (Right Shift assignment) | c >>= a same as c = c >> a |
For eg.
Source Code :
#include <stdio.h>
int main() {
int a=20,b=12;
b = a + b;
printf("value of b is %d\n",b);
b += a;
printf("value of b is %d\n",b);
b -= a;
printf("value of b is %d\n",b);
b *= a;
printf("value of b is %d\n",b);
b /= a;
printf("value of b is %d\n",b);
b %= a;
printf("value of b is %d\n",b);
b &= 2;
printf("value of b is %d\n",b);
b |= 2;
printf("value of b is %d\n",b);
b ^= 2;
printf("value of b is %d\n",b);
b <<= 2;
printf("value of b is %d\n",b);
b >>= 2;
printf("value of b is %d\n",b);
return 0;
}
Output
value of b is 32 value of b is 52 value of b is 32 value of b is 640 value of b is 32 value of b is 12 value of b is 0 value of b is 2 value of b is 0 value of b is 0 value of b is 0
6. Increment (++) and Decrement (--) with Prefix and Postfix
- Increment Operator (++) ये variable की value 1 से बढ़ा देता है |
- Decrement Operator (--) ये variable की value 1 से घटा देता है |
| Operators | Same as |
|---|---|
| ++a (Increment Prefix) | a = a + 1 |
| --a (Decrement Prefix) | a = a - 1 |
| a++ (Increment Postfix) | |
| a-- (Decrement Postfix) |
for eg.
Source Code :
#include <stdio.h>
int main() {
int a=20;
printf("Print Value with prefix : %d\n", ++a); // increase value with increment prefix
printf("Value of a : %d\n", a);
printf("Print Value with prefix : %d\n", --a); // decrease value with decrement prefix
printf("Value of a : %d\n", a);
printf("Print Value with postfix : %d\n", a++); // increase value with increment postfix
printf("Value of a : %d\n", a);
printf("Print Value with postfix : %d\n", a--); // decrease value with decrement postfix
printf("Value of a : %d\n", a);
return 0;
}
Print Value with prefix : 21 Value of a : 21 Print Value with prefix : 20 Value of a : 20 Print Value with postfix : 20 Value of a : 21 Print Value with postfix : 21 Value of a : 20
6. Conditional / Ternary Operator (?:)
- Conditional Operator में तीन Expressions होते है |
- Conditional Operator को Ternary Operator भी कहते है |
- Conditional Operator में अगर पहला expression true होता है, तो वो दूसरा expression output में print करता है |
- अगर Conditional Operator में पहला expression false होता है, तो वो तीसरा expression output में print करता है |
Syntax for Conditional / Ternary Operator
expression1 ? expression 2 : expression 3for eg.
Source Code :
#include <stdio.h>
int main()
{
int a = 100, b ;
b = ( a == 100 ? 2 : 0 ) ;
printf("Value of a is %d\n", a);
printf("Value of b is %d\n", b);
return 0;
}
Output :
Value of a is 100 Value of b is 2







No comments: