Create FREE 'HowTo' Videos with MyGuide

Operators



Pass Quiz and Get a Badge of Learning



Content "filtered", Please subscribe for FULL access.


Chapter 11 : Operators



Operators arrow_upward


  • Operators define how variables and literals in an expression will be manipulated.
  • Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result.
  • C supports several types of operators and they can be classified as:
    • Assignment Operators
    • Arithmetic Operators
    • Relational Operators
    • Unary Operators
    • Equality Operators
    • Conditional Operators
    • Bitwise Operators

    Assignment Operators arrow_upward


  • This operator is used for assigning a value to a variable.

  • Operator

    Example

    Assignment

    (=)

     int i=10;

    int j=i;


  • Program to demonstrate assignment operator.
  • Example:

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int a,b;
        clrscr();
       
         a = 53;
         printf("\n\t Value of A : %d",a);    // 53
         b = a; //assignment of value
         printf("\n\t Value of B : %d",b);    // 53
         getch();
    }
     

    Output :
    Value of A : 53
    Value of B : 53

    Arithmetic Operators arrow_upward


  • The primary arithmetic operators and their corresponding symbols in C:

  • Operator

    Example

    Addition (+)

    int i=8+9;

    Resultant value = 17

    Subtraction (-)

    int i=9-4;

    Resultant value = 5

    Multiplication (*)

    int i=8*6;

    Resultant value = 48

    Division (/)

    int i=10/2;

    Resultant value = 5

    Remainder (%)

    int i=10%3;

    Resultant value = 1


  • This simple program uses arithmetic operators:

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int a,b,c,d,e,f,g;
        clrscr();
        printf("\n\t Enter First Number :");    
        scanf("%d",&a); // 4
        printf("\n\t Enter Second Number :");   
        scanf("%d",&b); // 3
        c = a + b;
        printf("\n\t Addition is : %d",c);   // 7
        d = a - b;
        printf("\n\t Subtraction is : %d",d);  // 1
        e = a * b;
        printf("\n\t Multiplication is : %d",e);  // 12
        f = a / b;
        printf("\n\n\t Division is : %d",f); // 1
        g = a % b;
        printf("\n\t Modulus is : %d",g); // 1
        getch();
    } 
     

    Output :
    Enter First Number :1
    Enter Second Number :4
    Addition is : 5
    Subtraction is : -3
    Multiplication is : 4
    Division is : 0
    Modulus is : 1

    Relational Operators arrow_upward



    Operator

    Example

    Greater than

     (>)

    if (x>4)

    Less than

    (<)

    if (x<4)

    Greater than or equal to

    (>=)

    if (x>=4)

    Less than or equal to

    (<=)

    if (x<=4)


  • Program to demonstrate relational operators
  • Example:

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
         int a=6, b=2;
         clrscr();
         printf("\n A<=B : %d",(a<=b)); // 0 – False
         printf("\n A>B : %d",(a>b)); // 1 – True
         printf("\n A!=B : %d",(a!=b)); // 1 – True
         getch();
    }
     
    Output :
    A<=B : 0
    A>B : 1
    A!=B : 1

    Unary Operators arrow_upward



    Operator

    Example

    Unary plus

    (+)

    int i=+1

    Unary minus

     (-)

    int i=-1

    Increment

    (++)

    int j=i++

    Decrement

    (--)

    int j=i--

    Logical Not

    (!)

    boolean j=!true



    Equality Operators arrow_upward



    Operator

    Example

    Equality

    (==)

    if (i==1)

    Non equality

    (!=)

    if (i!=4)



    Conditional Operators arrow_upward



    Operator

    Example

    Conditional and

    (&&)

    if (a==4 && b==5)

    Conditional or

    (ll)

    if (a==4 ll b==5)


    Bitwise Operators arrow_upward



    Operator

    Example

    Bitwise AND

    (&)

    001 & 111=001

    Bitwise OR

    (l)

    001 l 110=111

    Bitwise XOR

    (^)

    001 ^ 110=111




    Thank You from Kimavi arrow_upward


  • Please email us at Admin@Kimavi.com and help us improve this tutorial.


  • Mark as Complete => Receive a Certificate in C-Programming


    Kimavi Logo

    Terms and conditions, privacy and cookie policy | Facebook | YouTube | TheCodex.Me | Email Kimavi


    Kimavi - An AI Powered Encyclopedia { Learning is Earning }

    Get Ad Free Encyclopedia with Progress Report, Tutor Help, and Certificate of Learning for only $10 a month



    All videos on this site created using MyGuide.

    Create FREE HowTo videos with MyGuide.