Operators In "C' Language:----
Operators एक symbol होते है जिनका उपयोग सी लैड्ग्वेज मे mathematical calculation और लॉजिकल डीसीजन लेने के लिए किया जाता है | इसकी सहायता से किसी variable या वैल्यू को ऑपरेट किया जाता है |उदाहरण के लिए + एक ऑपरेटर है जिसका उपयोग दो वैल्यू को जोड़ने के लिए किया जाता है |
Roperators एक या एक से अधिक वारियाबले कोंस्टंट या operands के साथ मिल कर कम करते है | सी लैड्ग्वेज मे कई बिल्ट इन operators पाये जाते है
Types Of Operators In C
1 Arithmatic Operators:- इस operators का उपयोग बेसिक mathematical operation perform करने के लिए किया जाता है
Symbols |
Operators |
Description |
+ |
Addition |
इस operators का उपयोग दो operands को जोड़ने
के लिए किया जाता है |
- |
Substraction |
इस operators का उपयोग दो operands को घटानेके
लिए किया जाता है |
* |
Multiplication |
इस operators का उपयोग दो operands को गुणा
के लिए किया जाता है |
/ |
Division |
इस operators का उपयोग दो operands को भाग के
लिए किया जाता है |
% |
Modulo Division |
इस operators का उपयोग दो operands को शेष्फल
के लिए किया जाता है |
#include <stdio.h>
void main() {
int num1, num2, sum, sub, mul, div, mod;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Perform arithmetic operations
sum = num1 + num2;
sub = num1 - num2;
mul = num1 * num2;
div = num1 / num2;
mod = num1 % num2;
// Print the results
printf("Sum = %d\n", sum);
printf("Difference = %d\n", sub);
printf("Product = %d\n", mul);
printf("Quotient = %d\n", div);
printf("Remainder = %d\n", mod);
}
2 Relational Operators
Symbols |
Operators |
Description |
> |
Greater then |
इस operators का उपयोग दो operands को बीच greater
चेक करने के लिए किया जाता है अगर first operands की value second operands से
बड़ी है तो true वैल्यू रिटर्न करता है नहीं तो false
value return करता है |
< |
Less then |
इस operators का उपयोग दो operands को बीच लेस
चेक करने के लिए किया जाता है अगर first operands की value
second operands से छोटी है तो true वैल्यू रिटर्न करता है नहीं तो false value return करता है |
>= |
Greater then equal to |
इस operators का उपयोग दो operands को बीच greater
और बराबर चेक करने के लिए
किया जाता है अगर first operands की value second operands से बड़ी या बराबर है तो true वैल्यू रिटर्न
करता है नहीं तो false value return करता है |
<= |
Less then equal to |
इस operators का उपयोग दो operands को बीच less
और बराबर चेक करने के लिए
किया जाता है अगर first operands की value second operands से छोटी या बराबर है तो true वैल्यू रिटर्न
करता है नहीं तो false value return करता है |
== |
equal to |
इस operators का उपयोग दो operands की वैल्यू
बराबर है की नहीं चेक करने के लिए किया जाता है अगर दोनों operands की वैल्यू बराबर है तो true return करेगा नहीं टी
false retusn करेगा |
!= |
Not equal to |
इस operators का उपयोग दो operands की वैल्यू
बराबर नहीं है चेक करने के लिए किया जाता
है अगर दोनों operands की वैल्यू बराबर नहीं है तो true
return करेगा और बराबर है तो false retusn करेगा |
3 Logical Operators
Logical Operators का उपयोग relational operator को जोड़कर एक complex expression को बनाने के लिए किया जाता है इस ऑपरेटर को solve करने पर जो मान आता है वह हमेश लॉजिकल होता है
Symbols |
Operators |
Description |
&& |
Logical AND |
Truth Table |
|| |
Logical OR |
Truth Table |
! |
Logical NOT |
Truth Table |
उपर दिये गए table मे Logical AND और Logical OR को binary operator के रूप मे उपयोग करते है तथा Logical NOT unary operator के रूप मे उपयोग करते है क्यूंकी इसमे सिर्फ एक operand की आवश्यकता पढ़ती है
लॉजिकल operators को समझने के लिए हमे Truth Table की आवश्यकता पढ़ती है
Truth Table
a |
b |
a && b |
a || b |
! a |
! b |
1 |
1 |
1 |
1 |
0 |
0 |
1 |
0 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
Example:- int a,b,c;
a=10; b=20; c=30;
(a>b) && (c<b)
10>20 && 30<20
F && F
output : F
4 Conditional operators :
Syntax:- (condition ? expression1 : expression1 )
int a,b;
a=10;
b=20;
((a>b) ? printf("a is big number"); : printf("b is big number"); )
Example
#include <stdio.h>
void main(){
int a, b, max;
printf("Enter three integers: ");
scanf("%d %d", &a, &b);
// Conditional operator to find the maximum of a, b, and c
max = (a > b) ? a : b;
printf("The maximum of %d and %d is: %d\n", a, b,max);
}
Enter three integers: 100
50
The maximum of 100 and 50 is: 100
5 Assignment Operators :---
इसे हम निम्न उदाहरण के द्वारा समझ सकते है :---
यदि int m=25 है तो
Operators |
Meaning |
Example |
Result |
+= |
m+=10 |
m=m+10 |
m=35 |
-= |
m-=10 |
m=m-10 |
m=15 |
*= |
M*=10 |
m=m*10 |
m=250 |
/= |
m/=10 |
m=m/10 |
m=2 |
%= |
M%=10 |
m=m%10 |
m=5 |
#include <stdio.h>
void main() {
int x, y;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of y: ");
scanf("%d", &y);
// Assignment operator (x = y)
x = y;
printf("The value of x after assignment: %d\n", x);
}
Enter the value of x: 30
Enter the value of y: 20
The value of x after assignment: 20
6 Increment and Decrement operators:-
7 Bitwise Operators
Bitwise
Operators in C:
Bitwise
operators in C manipulate individual bits within integers. They are essential
for tasks like low-level programming, cryptography, and optimization. Here's a
breakdown of the available operators:
1.
Bitwise AND (&):
- Performs logical AND on corresponding bits of two operands.
- If both bits are 1, the result is 1; otherwise, it's 0.
- Example:
C
भाषा
में बिटवाइज़ ऑपरेटर
C
भाषा
में बिटवाइज़ ऑपरेटर एक संख्या के बिट्स पर संचालन करने के लिए उपयोग किए जाते
हैं। इन ऑपरेटरों का उपयोग low-level programming, क्रिप्टोग्राफी और optimization जैसे कार्यों के लिए
किया जाता है।
उपलब्ध बिटवाइज़ ऑपरेटर:
- बिटवाइज़ एंड (&):
- दो ऑपरेंड के संगत बिट्स पर तार्किक AND करता है।
- यदि दोनों बिट्स 1 हैं, तो परिणाम 1 है; अन्यथा, यह 0 है।
- उदाहरण:
int x = 5; // 0101
int y = 3; // 0011
int z = x & y; // 0001 (1)
2.
Bitwise OR (|):
- Performs logical OR on corresponding bits of two operands.
- If at least one bit is 1, the result is 1; otherwise, it's 0.
- Example:
2.बिटवाइज़ OR (|):
- दो ऑपरेंड के संगत बिट्स पर तार्किक OR करता है।
- यदि कम से कम एक बिट 1 है, तो परिणाम 1 है; अन्यथा, यह 0 है।
- उदाहरण:
int x = 5; // 0101
int y = 3; // 0011
int z = x | y; // 0111 (7)
3.
Bitwise XOR (^):
- Performs logical XOR (exclusive OR) on corresponding bits of two
operands.
- If the bits are different, the result is 1; otherwise, it's 0.
- Example:
बिटवाइज़ XOR (^):
- दो ऑपरेंड के संगत बिट्स पर तार्किक XOR (एक्सक्लूसिव OR) करता है।
- यदि बिट्स अलग हैं, तो परिणाम 1 है; अन्यथा, यह 0 है।
- उदाहरण:
int x = 5; // 0101
int y = 3; // 0011
int z = x ^ y; // 0110 (6)
4.
Bitwise NOT (~):
- Inverts all bits of an operand.
- 0 becomes 1, and 1 becomes 0.
- Example:
बिटवाइज़ NOT (~):
- एक ऑपरेंड के सभी बिट्स को इनवर्ट करता है।
- 0 बन जाता है 1, और 1 बन जाता है 0।
- उदाहरण
int x = 5; // 0101
int z = ~x; // 1010 (-6)
5.
Bitwise Left Shift (<<):
- Shifts the bits of an operand to the left by a specified number of
positions.
- Zeros are filled in from the right.
- Example
बिटवाइज़ लेफ्ट शिफ्ट (<<):
- एक ऑपरेंड के बिट्स को निर्दिष्ट संख्या में स्थिति से
बाईं ओर शिफ्ट करता है।
- दाईं ओर से शून्य भरे जाते हैं।
- उदाहरण
int x = 5; // 0101
int z = x << 2; // 0100 (20)
6.
Bitwise Right Shift (>>):
- Shifts the bits of an operand to the right by a specified number
of positions.
- The behavior of the sign bit depends on the implementation:
- Arithmetic shift: The sign bit is preserved.
- Logical shift: Zeros are filled in from the left.
- Example:
बिटवाइज़ राइट शिफ्ट (>>):
- एक ऑपरेंड के बिट्स को निर्दिष्ट संख्या में स्थिति से
दाईं ओर शिफ्ट करता है।
- साइन बिट का व्यवहार कार्यान्वयन पर निर्भर करता है:
- अंकगणितीय शिफ्ट: साइन बिट
संरक्षित रहता है।
- तार्किक शिफ्ट: बाईं ओर से
शून्य भरे जाते हैं।
- उदाहरण:
int x = -5; // 1011
int z = x >> 2; // 0010 (-3) (assuming arithmetic shift)
Example
#include <stdio.h>
void main() {
int x = 10, y = 5;
// Bitwise AND (&)
int andResult = x & y;
printf("Bitwise AND: %d\n", andResult);
// Bitwise OR (|)
int orResult = x | y;
printf("Bitwise OR: %d\n", orResult);
// Bitwise XOR (^)
int xorResult = x ^ y;
printf("Bitwise XOR: %d\n", xorResult);
// Bitwise NOT (~)
int notX = ~x;
printf("Bitwise NOT of x: %d\n", notX);
// Bitwise left shift (<<)
int leftShiftResult = x << 2;
printf("Bitwise left shift: %d\n", leftShiftResult);
// Bitwise right shift (>>)
int rightShiftResult = x >> 2;
printf("Bitwise right shift: %d\n", rightShiftResult);
}
Output
Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT of x: -11
Bitwise left shift: 40
Bitwise right shift: 2
8 Special Operators
Operator Precedence and associativity in C
Associativity in C
दोनों "/" (विभाजन) और "*" (गुणन)
ऑपरेटरों की समान प्राथमिकता होती है, इसलिए मूल्यांकन का क्रम
साहचर्य द्वारा तय किया जाएगा।
तालिका के अनुसार, गुणात्मक ऑपरेटरों का साहचर्य बाएं से दाएं है। इसलिए, अभिव्यक्ति का मूल्यांकन इस प्रकार किया जाता है -
(15/5)*2
3*2=6
#include
<stdio.h>
Void main()
{
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d;
printf("e: %d\n",
e);
}
Output
Run the code and check its output −
e: 90
In this expression, the addition of a and b in parenthesis is first.
The result is multiplied by c and then the division by d takes place.
No comments:
Post a Comment