What is Operators in C Language and How Does It Work?



OPERATORS IN C LANGUAGE



Operator in C programming Language : In this page we will learn about What is Operators in C? What are different types of Operators in C? and also explaining each operators with its proper example program with its output. After reading this page you are completely aware of Operators in C and its different types.


What Is Operators in C Language and How Does It Work?
                                                       


    An Operator in C language is a symbol having specific meaning which tells the compiler to perform arithmetical calculation such as addition, subtraction, multiplication, division etc  as well as logical problems such as comparison. C language is rich in built in operators and provides different types of operator having special function to each operator.

    Types of Operators in C language:


    • Unary Operators
    • Arithmetic Operators
    • Bitwise Operators
    • Relational Operators
    • Logical Operators
    • Conditional Operators
    • Assignment Operators

    Unary Operators


    Unary Operators are symbol that performs operation on single operand. It manipulates single operand and gives new value as a result. It is of different types such as  ++,  --,  sizeof()

    int main()
    {
        int x=1;
        x=++x;
        printf("Value of x after operation is %d\n", x);
    
        int y=1;
        y= --y;
        printf("\nValue of y after operation is %d\n", y);
    
        int z;
        z= 1;
        printf("\nSize of z is %d\n", sizeof(z));
    
        return 0;
    }
    
    Value of x after operation is 2
    
    Value of y after operation is 0
    
    Size of z is 4

    Arithmetic Operators


    Arithmetic Operators are symbol that perform operation on more two operand. It does arithmetical calculations such addition, subtraction, multiplication, division, modulus. It is of different types having symbol like +,  -,  %,  /,  * .  

    int main()
    {
        int amount, hundred, rupee;
        printf("Enter any amount to convert it into multiple of hundred:\n");
        scanf("%d",&amount);
        hundred=amount/100;
        rupee=amount%100;
        printf("\n%d Hundred and %d Rupees",hundred,rupee);
        return 0;
    }
    
    Enter any amount to convert it into multiple of hundred:
    5029
    
    50 Hundred and 29 Rupees

    Bitwise Operators


    Bitwise Operators are symbol that perform special type of operation on one or more than one operand. It takes one or two operand as input and perform operation and gives result. 

    It is of different types given below:


    • Bitwise AND ( & )
    • Bitwise OR ( | )
    • Bitwise XOR ( ^ )
    • Bitwise NOT ( ~ )
    • Right shift ( >> )
    • Left shift ( << )

    int main()
    {
        int a = 5, b = 9;
        printf("Performing Bitwise Operation on operand a and b:\n\n");
        printf("\nValue of a = %d and b = %d\n\n", a, b);
        printf("a&b\t= %d\n", a, b);
        printf("a|b\t= %d\n", a|b);
        printf("a^b\t= %d\n", a^b);
        printf("~a\t= %d\n", a=~a);
        printf("b<<1 b="" printf="" t="%d\n">>1\t= %d\n", b>>1);
        return 0;
    }
    
    Performing Bitwise Operation on operand a and b:
    
    
    Value of a = 5 and b = 9
    
    a&b     = 5
    a|b     = 13
    a^b     = 12
    ~a      = -6
    b<<1 18="" b="">>1    = 4

    Relational Operators


    Relational Operators are symbol that perform operation on two operand. It does comparisonal operation between two operand and gives Boolean results True or False.

    It is of different types given below:

    • Less than ( < )
    • Greater than ( > )
    • Less than Equal to ( <= )
    • Greater than Equal to ( >= )
    • Not Equal to ( != )

    int main()
    {
        int x, y;
        printf("Enter two values to check which is greater:\n");
        scanf("%d %d", &x, &y);
        printf("\nEntered value in x and y through keyboard are %d and %d.\n\n", x, y);
        if(x>y)
            printf("%d is greater than %d", x, y);
        else
            printf("%d is greater than %d", y, x);
        return 0;
    }
    
    Enter two values to check which is greater:
    9
    11
    
    Entered value in x and y through keyboard are 9 and 11.
    
    11 is greater than 9

    Logical Operators


    Logical Operators are the symbol that perform operation on one or two operand. It does logical calculations and produces results of Boolean values True or False.


    It is of different types given below:

    • NOT ( ! )
    • AND ( && )
    • OR (||)

    int main()
    {
        int yr;
        printf("Enter the year to check the year is leap or not:\n");
        scanf("%d",&yr);
        if(yr%400==0)
            printf("Entered year %d is the leap year",yr);
        else if(yr%4==0&&yr%100!=0)
            printf("Entered year %d is the leap year",yr);
        else
            printf("Entered year %d is not the leap year",yr);
        return 0;
    }
    
    Enter the year to check the year is leap or not:
    2020
    Entered year 2020 is the leap year

    Conditional Operators


    Conditional Operators is a combination of ? and : symbol and it is also called ternary operator since they take three arguments. 

    syntax of Conditional operator:

    statement 1 ? statement 2 : statement 3

    The above statements says that if statement 1 is true then the value returned will be statement 2 otherwise the value returned will be statement 3.

    int main()
    {
        int num;
        printf("Enter a value to check whether it is Even or Odd:\n");
        scanf("%d", &num);
        num%2==0 ? printf("\nEntered value %d is even",num) : printf("\nEntered value %d is Odd",num);
        return 0;
    }
    
    Enter a value to check whether it is Even or Odd:
    9
    
    Entered value 9 is Odd

    Assignment Operators


    Assignment Operators are the symbol which perform operations like assignment of one values to another variable. It is of different types such as = , += , -=  , *=  , /=   , %= .

    int main()
    {
        int x=4;
        x*=5;
        printf("Result = %d", x);
        return 0;
    }
    
    Result = 20

    Conclusion:

    In this article, we have thoroughly read about Operators in C programming Language with brief description of each types of Operators in C . Types of  Operators in C are:

    • Unary Operators
    • Arithmetic Operators
    • Bitwise Operators
    • Relational Operators
    • Logical Operators
    • Conditional Operators
    • Assignment Operators

    If you find this page helpful then please share!


    Tags: Operators in C, Types of Operators in C, Sample programs of Operators in C with its output,
    Operators in C programming Language


    No comments:

    Powered by Blogger.