C Operators

Operators are special symbols that do some predefined task on the operands.
They are used to performing operations like arithmetic, logical, etc.

There are following three types of operators in C language.

  1. Unary Operators
  2. Binary Operators
  3. Ternary Operators

types of operators

Unary Operators

Unary Operators are the operators which require single operand to perform any action.

Examples of unary operator

  1. sizeof: return the size of an operand in bytes.
    Example:
  2. increment operator (++): This operator adds 1 to its operand.
    Example

    increment operator is divided into two types

    • post-increment operator –

      In this statement first the value of ‘a’ will be printed on screen and then the value of ‘a’ incremented by 1.
    • pre-increment operator –

      In this statement first the value of ‘a’ will be incremented by 1 and then the incremented value will be printed on screen.
  3. decrement operator (–): This operator subtracts 1 from its operand.
    Example:

    decrement operator is divided into two types

    • post-decrement operator –

      In this statement first the value of ‘a’ will be printed to screen and then the value of ‘a’ decremented by 1.
    • pre-decrement operator –

      In this statement first the value of ‘a’ will be decremented by 1 and then the decremented value will be printed on the screen.
  4. unary minus operator: unary minus negate the value.
    Example:

Binary Operators

Binary operators are the operators which require two operands to perform any action.
C offers different types of binary operators. they are given as follows

  • Arithmetic Operators
  • Logical Operators
  • Relational Operators
  • Bitwise Operators
  • Assignment Operators
  • Special Operators

Arithmetic Operators

These operators are used to perform arithmetic/mathematical operations.
Below table describes all arithmetic operators.
Assume a = 20 and b = 10.

 

Operator Name Description Example
       + Addition Adds two operands a + b = 30
       – Subtraction Subtracts two operands a – b = 10
       * Multiplication Multiplies two operands a * b = 200
       / Division Divides first operand by second a / b = 2
      % Modulus Returns the remainder of division a % b = 0

Relational Operators

Relational operators are used to compare two values.
The output of relational operators is boolean values i.e. true or false.
Following table describes all relational operators supported by C language
(assume a=5 and b=6)

Operator Name Description Example Output
       = = Is equal to Checks whether the two operands are equal or not. If so, it returns true. Otherwise, it returns false. a = = b false
       != Is not equal to Checks whether the two operands are equal or not. If so, it returns true. Otherwise, it returns false. a ! = b true
       > Greater than Checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false a > b false
       < Less than Checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise, it returns false. a < b true
       >= Greater than or equal to Checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise, it returns false. a > = b false
       < = Less than or equal to Checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise, it returns false. a < = b true

Logical Operators

This operator is used to test more than one condition.
C supports 3 logical operators.
Following table describes all logical operators with example
(assume x = 5 and y = 10)

Operator Name Description Example Output
       && logical AND It returns true when both conditions are true (x>5)&&(y<5) false
        || logical OR It returns true when at-least one of the condition is true (x>=5)||(y>=10) true
         ! logical NOT It is used to reverse state of its operand. If a condition is true, then Logical NOT operator will make it false. !((x>5)&&(y<5)) true

Bitwise Operators

In C, there are 6 bitwise operators.
Consider a = 10 and b = 2 then their binary conversion is
a = 1010
b = 0010
Following is the list of bitwise operators.

Operator Name Description Example Output
       & Bitwise AND The result of a&b is 1 only if both bits are 1. a & b (1010 & 0010) 2
       | Bitwise OR The result of OR is 1 any of the two bits is 1 a | b 10
       ^ Bitwise XOR The result of XOR is 1 if the two bits are different. a ^ b 8
      >> Binary Right Shift The left operands value is moved right by the number of bits specified by the right operand. a >> 2 2
      << Binary Left Shift The left operands value is moved left by the number of bits specified by left operand a << 2 40
       ~ Binary One’s Complement Inverts all bits. ~ a 5

Assignment Operators

It is the value assigning operator. After evaluating the expression on the right-hand side, it assigns the value to the variable on the left-hand side.
Following is list of assignment operators (let A = 10)

 

Operator Name Example
       = Assignment operator marks = 10
      += Increment, then assign A += 2 (i.e. A = A + 2) A = 12
      – = Decrement, then assign A -= 2 (i.e. A = A – 2) A = 8
      * = Multiply, then assign A *= 2 (i.e. A = A * 2) A = 20
      /= Divide, then assign A /= 2 (i.e. A = A / 2) A = 5
      %= Modulus, then assigns A %= 2 (i.e. A = A % 2) A = 0
      <<= Left shift and assigns A <<= 2 (i.e. A = A << 2) 40
      >>= Right shift and assigns A >>= 2 (i.e. A = A >> 2) 2
     &= Bitwise AND assigns A &= 2 (i.e. A = A & 2) 2
     ^= Bitwise exclusive OR and assigns A ^= 2 (i.e. A = A ^ 2) 8
     |= Bitwise inclusive OR and assigns A |= 2 (i.e. A = A | 2) 10

Special Operators

There are two special operators in C language given in following table

   Operator Description Example
        & It returns the address of a variable &a
         * It is used as pointer to a variable *a

Ternary or conditional Operator ( ? : )

  • There is one ternary operator supported by C language
  • The ternary operator is also called as “Conditional Operator”.
  • Syntax: Condition ? expression1 : expression2
    If condition is true expression1 gets executed else expression2.
  • A ternary operator is a short form of if-else.

Example:

  • Ternary operator can be nested as following:
    Condition ? expression1(condition1 ? exp 1 :exp 2) : expression2 (condition2 ? exp 1 :exp 2)
  • For example:
    int big, a, b, c ;
    big = ( a > b ? ( a > c ? 3: 4 ) : ( b > c ? 6: 8 ) ) ;

 

Help others by sharing the content!

6 thoughts on “C Operators”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.