Operators in c


An operator in a programming language is a symbol that tells the compiler or interpreter to perform a specific mathematical, relational or logical operation and produce a final result.

Types of operators in c

  • Relational Operators.
  • Conditional Operators.
  • Bitwise Operators.
  • Logical Operators.
  • Arithmetic Operators.
  • Assignment Operators.
  • Increment / Decrement Operators.

Relational Operators

  • Relational operators check if a specific relation between two operands is true.
  • The result is evaluated to 1 (which means true) or 0 (which means false).
  • This result is often used to affect control flow (via if, while, for), but can also be stored in variables.
  • Relational operators evaluate values on the left and right side of the operator and return the relation as either True or False.
  • Operator Description
    == is equal to
    < is less than
    > is greater than
    <= is less than or equal to
    >= is greater than or equal to
    != is not equal to

Conditional Operators

  • Evaluates its first operand, and, if the resulting value is not equal to zero, evaluates its second operand. Otherwise, it evaluates its third operand, as shown in the following example:
  •  a = b ? c : d;
    is equivalent to:
    
    if (b)
     a = c;
    else
     a = d;
    

Bitwise Operators

  • C supports a set of bitwise operators. These operators can operate only on integer operands such as int, char, short, long in etc.
  • Operator Description
    >> Right shift
    << Left shift
    ^ Bitwise xor
    ~ One's complement
    & Bitwise AND
    | Bitwise OR

Logical Operators

  • The logical relationship between the two quantities is checked with logical operators.
  • Using these operators, two expressions can be joined and conditions can be checked. Such an expression provides result either logically true (1) or false (0). The operands could be constants, variables and expressions.
  • The logical operators are used to support the basic logical operations AND, OR, and NOT.
  • Operator Description
    && AND
    || OR
    ! NOT

Arithmetic Operators

  • Return a value that is the result of applying the left hand operand to the right hand operand, using the associated mathematical operation. Normal mathematical rules of commutation apply (i.e. addition and multiplication are commutative, subtraction, division and modulus are not).
  • Operator Description
    + Addition
    - Subtraction
    * Multiplication
    / Division
    % Modulus
    ++ Increment
    -- Decrement

Assignment Operators

  • Assigns the value of the right-hand operand to the storage location named by the left-hand operand, and returns the value.
  • var = expression;
    
    int x, y, z;
    x = y = z = 100; // set x, y, and z to 100

Increment / Decrement Operators

  • The increment and decrement operators exist in prefix and postfix form.
  • int a = 1;
    int b = 1;
    int tmp = 0;
    
    tmp = ++a; /* increments a by one, and returns new value; a == 2, tmp == 2 */
    tmp = a++; /* increments a by one, but returns old value; a == 3, tmp == 2 */
    tmp = --b; /* decrements b by one, and returns new value; b == 0, tmp == 0 */
    tmp = b--; /* decrements b by one, but returns old value; b == -1, tmp == 0 */