Operators In C Language By:- Sapan Das - .OnlineGuruji

.OnlineGuruji

Provides E-Notes,Video Tutorials And Download Links For Students In hindi

*********************************************************

*********************************************************
********************************************************************************

Breaking

Ramchandicollege Saraipali

Technology Jobs

OnlineGuru Blog मे आपका स्वागत है ब्लॉग से संबन्धित जानकारी के लिए संपर्क करें मो ॰ नम ।-9826026747(सपन कुमार दास) अपने विषय से संबन्धित अपडेट प्राप्त करने के लिए ब्लॉग पे दिये गए Bell Icon को press करें कम्प्युटर,Science,English Grammer से संबन्धित विषय की अधिक जानकारी के लिए हमारे ब्लॉग अथवा यू ट्यूब चैनल को सब्क्राइब करे ..

Monday, January 25, 2021

Operators In C Language By:- Sapan Das

 


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);

}

Output
Enter the first number: 100
.Enter the second number: 50
Sum = 150
Difference = 50
Product = 5000
Quotient = 2
Remainder = 0

2 Relational Operators

इस ऑपरेटर का प्रयोग प्रोग्राम मे दिये गए वैल्यू के बीच रेलतीओन चेक करने के लिए किया जाता है इसका output हमे लॉजिकल प्रपट होता है लॉजिकल मान या तो true होगा या false होगा 

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 करेगा                               

#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);

    // Relational operators
    printf("x == y: %d\n", x == y);
    printf("x != y: %d\n", x != y);
    printf("x > y: %d\n", x > y);
    printf("x < y: %d\n", x < y);
    printf("x >= y: %d\n", x >= y);
    printf("x <= y: %d\n", x <= y);

}
output:-
Enter the value of x: 10
Enter the value of y: 20
x == y: 0
x != y: 1
x > y: 0
x < y: 1
x >= y: 0
x <= y: 1

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 :

इस operators को ternary operators के नाम से भी जाना जाता है इस operators  मे तीन भाग होते है जो ? और : से divide रहते है पहले भाग मे कंडिशन चेक होता है अगर दिया गया कंडिशन सही है दूसरा भाग execute होता है नहीं तो तीसरा भाग execute  होता है 

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 :--- 

इस ऑपरेटर को "=": symbol से दर्शाते है इसका उपयोग किसी variable मे value को assign करने के लिए किया जाता है जैसा की हम जानते है की किसी एक्स्प्रेश्न मे दो भाग होते है LHS और RHS और दोनों भाग के बीच हम assignment oprator का उपयोग करते है अर्थात RHS के equation को solve करने के बाद जो value output के रूप मे प्रपट होता है उसे LHS के variable मे स्टोर करने के लिए assignment oprator का उपयोग होता है arithmatic opratpr के साथ assignment oprator का उपयोग होता है 

इसे हम निम्न उदाहरण के द्वारा समझ सकते है :---

यदि 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:-

यह operator c का मुख्य operator है समान्यतः दूसरे लैड्ग्वेज मे यह ऑपरेटर नहीं पाया जाता है increment operator को ++ तथा decrement operator को -- symbol से दर्शाया जाता है इन operarors का उपयोग किसी variable की value को एक one increment अथवा एक one decrement करने के लिए किया जाता है इन operarors को variable के पहले अथवा variable बाद दोनों ही स्थिति मे उपयोह किया जाता है अगर वारियाबले के पहले करते है तो prefix और यदि variable के बाद करते है तो postfix increment/decrement ऑपरेटर कहलाता है 
prefix increment/decrement  ++m /--m
postfix increment/decrement m++/m--
pre और post मे एक मुख्य अंतर होता है प्री इंक्रेमेंट मे पहले 1 को ओपेरेंड़ मे जोड़ता है और फिर आन्सर को left side के variable मे assign करता है जबकि पोस्ट इंक्रेमेंट पहले left side के variable मे assign करता है और फिर ओपेरेंड़ मे जोड़ता है

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d,e;
a=10;
b=a++;
printf("%d",b);
c=10;
d=++c;
printf("%d",d);
getch();
}
आउटपुट:-
10 11 

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 जैसे कार्यों के लिए किया जाता है।

उपलब्ध बिटवाइज़ ऑपरेटर:

  1. बिटवाइज़ एंड (&):
    • दो ऑपरेंड के संगत बिट्स पर तार्किक 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

सी में एक ही अभिव्यक्ति में विभिन्न प्रकार के कई ऑपरेटर हो सकते हैं। सी कंपाइलर ऑपरेटरों के प्राथमिकता और साहचर्य के आधार पर इसका मूल्य मूल्यांकन करता है। ऑपरेटरों की प्राथमिकता यह निर्धारित करती है कि अभिव्यक्ति में उनका मूल्यांकन किस क्रम में किया जाता है। उच्च प्राथमिकता वाले ऑपरेटर पहले मूल्यांकन किए जाते हैं। उदाहरण के लिए, इस अभिव्यक्ति पर एक नज़र डालें -


x=7+3*2; 
यहाँ, गुणन ऑपरेटर "*" का योग ऑपरेटर "+" की तुलना में उच्च प्राथमिकता है। इसलिए, पहले गुणन 3 * 2 किया जाता है और फिर 7 में जोड़ा जाता है, जिसके परिणामस्वरूप "x = 13" होता है।

Associativity in C

सी में ऑपरेटरों का साहचर्य एक अभिव्यक्ति के भीतर एक प्रोग्राम का मूल्यांकन की दिशा (बाएं से दाएं या दाएं से बाएं) को संदर्भित करता है। ऑपरेटर साहचर्य का उपयोग तब किया जाता है जब एक अभिव्यक्ति में समान प्राथमिकता के दो ऑपरेटर दिखाई देते हैं। निम्नलिखित उदाहरण में -(15/5)*2   3*2=6
15/5*2

दोनों "/" (विभाजन) और "*" (गुणन) ऑपरेटरों की समान प्राथमिकता होती है, इसलिए मूल्यांकन का क्रम साहचर्य द्वारा तय किया जाएगा।

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

(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