Translate

Saturday, April 18, 2020

Operators in C

/*Operators And Expressions*/

An operators specifies an operation to be performed that yields a value. operators are essential to form expression and to perform arithmetical and logical operations on  values or variables. some operators require two operands, while other act upon only one operand. An operand is a data item on which an operator acts.
C includes types of operators. which are as-






  • Arithmetic operators
  • Assignment operators
  • Increment and Decrements operators
  • Relational operators
  • Logical operators
  • Conditional operators
  • Comma operators
  • Sizeof operator
  • Bitwise operators
  • Other operators

  • 1.) Arithmetic Operators 

    Arithmetic operators are used for arithmetic and numeric calculations which perform addition, subtractions, multiplication etc. they are of two types...

    i. Unary arithmetic operators
    ii. Binary arithmetic operators
    • Unary Arithmetic Operators: unary operators require only one operand. 
    example: +x -y
    • Binary Arithmetic Operators
    Binary operators require two operands. 
    example: 5+4-9
    The "+" and "-" symbols are called operators and 5,4,9 are the operands.

        Operators
       Purpose
       +   
       Addition
     -
       Subtraction
     *
       Multiplication
     /  
       Division
    %
        Remainder

    Example of arithmetic Operators:

    #include<stdio.h>
    #include<conio.h>
     void main()
    {
    int x, y, result;
    printf("Enter your first integer in x =");
    scanf("%d",&x);
    printf("Enter your second integer in y =");
    scanf("%d",&y);
    //Addition
    result=x+y;
    printf("\n addition = %d",result); 
    //Subtraction
    result =x-y;
    printf("\nSubtraction = %d",result);     
    //division
    result=x/y;
    printf("\ndivision = %d",result); 
    //Remainder
    result=x%y;
    printf("\nremainder after division = %d",result);
    getch();
    }
    Ouput:









    2.) Assignment Operators

    The assignment operator is represented by equal sign(=). it is assign a value.
    example: a=10
    Here, 10 is assigned to a.

    3.) Increment and decrements Operators

    The increment and decrements operators in C are represented by ++ and -- sign. The operator "++" means add 1 and the operators "--" means subtract 1.
    for example: 
    int a=5; 
    a=a+1; / ++a;
    Result a=6
    int a=5; 
    a=a-1; / --a;
    Result a=4
    These operators are two type-
    i.) Prefix increments/decrements:  Here, operator is written before the operand. e.g. ++a or --a.
    ii.) Postfix increment/decrements: Here, operators is written after the operand. e.g. a++ or a--.

    For example:
    #include<stdio.h>
    #include<conio.h>
     void main()
    {
    int x ;
    printf("Enter your first integer in x =");
    scanf("%d",&x);
    printf("\n value of x = %d",x);  
    //prefix increment ++x
    printf("\n prefix increment = %d",++x);
    printf("\n after prefix increment the value of x = %d",x); 
    //prefix decrements --x
    printf("\n prefix decrements= %d",--x); 
    printf("\n after prefix decrements the value of x = %d",x); 
    //postfix increment x++
    printf("\n postfix increment = %d",x++);
    printf("\n after postfix increment the value of x = %d",x);
    //postfix decrement x--
    printf("\n postfix decrements = %d",x--); 
    printf("\n after postfix decrements the value of x %d",x); 
    getch();

    }
    Output:












    4.)Relational Operators


    relational operators such as greater than or less than, are used to compare value between two variables.an expression contains relational operators is called relational expression. the relational operators are.

    OperatorsPurpose
    <less than
    <=less than or equal to
    ==equal to
    !=not equal to
    >greater than
    >=greater than or equal to

    5.) Logical Operator

    An expression that combines two or more expression is termed as a logical expression. for combining these expression we used logical operators.
    C allow three types of logical operators, namely 
    OperatorsPurpose
     &&Logical And
    ||Logical Or
    !Logical Not

    6.) Conditional Operator

    The conditional operator(? and :) require three expression as operands. it is also called ternary operator. the syntax or the conditional operator is 
    conditional expression ? expression1 : expression2;
    void main()
    {
     int x,y;
    if(x>y)? a:b;
    }
    a is true and b is false.

    7.) Comma Operators

    The expression is separated by the comma operator.
    for example: x=5, b=9;  

    8.) sizeof Operator

    sizeof Operator is used to return the size of its operand in terms of bytes. it is an unary operator.

    9.) Bitwise Operator

    bitwise operators operate on integers only.

    OperatorsPurpose
    &bitwise AND
    |bitwise OR
    ~one's complement
    <<left shift
    >>right shift
    ^bitwise XOR






    No comments:

    Post a Comment

    C program to make a calculator using the switch statement.

    /*perform arithmetic calculation on integers in C language*/ #include<stdio.h> #include<conio.h> void main() {     char op;     ...