Operators in C

An expression consists of variables and constants separated by operators. C language uses 7 types of operators as listed below:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Increment and decrement operators
  • Assignment Operators
  • Conditional Operators
  • Bitwise Operators

    1. Arithmetic Operators : Arithmetic Operators are used to perform arithmetic operations.

    OperatorMeaningExampleResult
    +addition4+26
    -subtraction4-22
    *multiplication4*28
    /division4/22
    %modulus4%20

    2. Relational Operators : Relational Operators are used to compare the values of operands or expressions to produce a logical value. A logical is either true or false.

    OperatorMeaningExampleResult
    <less than4<3false
    >greater than4>3true
    <=less than or equal to4<=3false
    >=greater than or equal to4>=3true
    (==)equal to4==3false
    !=not equal to4!=3true

    3. Logical Operators : Logical Operators are used to connect more relational operations to form a complex expression that is called Logical Operators or Logical expression. A value obtained by evaluating a logical expression is always logical either true or false.

    OperatorMeaningExampleResult
    &&logical and(4<3)&&(4>3)false
    ||logical or(4<2) || (4>3)true
    !logical not! (4<2)true

    4. Increment and Decrement operators (++ and – –) : Increment operator (++) is used to increase the value of an integer or char variable by 1. Decrement operator (- -) is used to reduce the value of an integer or char variable by 1. Consider the following examples :

    a = 25;
    a++ or ++a will produce the result a = 26
    a = 25;
    a- – or – -a will produce the result a = 24

    5. Assignment Operators : Assignment Operators are used to perform arithmetic operations while assigning a value to a variable.

    6. Conditional Operators or Ternary Operators (?:) : Conditional Operators is used to check a condition and select a value depending on the value of the condition. Normally the selected value will be assigned to a variable which has the following form :

    variable=(condition)?value1:value2;
    

    Example :

    big=(x>y)?x:y;
    

    7. Bitwise Operators : Bitwise Operators are used to perform operations at a binary digit level. These operators are not commonly used and used only in special applications where optimized use of storage is required.

    OperatorMeaning
    <<Shifts the bits to left
    >>Shifts the bit to right
    ~Bitwise inversion
    &Bitwise logical and
    |Bitwise logical or
    ^Bitwise exclusive or