Operators in C

Filed Under: C Programming

Operators are essential components in programming for logical and mathematical functions. Operators in C programming are categorised in following ways.

Operators in C

Operators in C

Arithmetic Operators in C

Binary Arithmetic operators

These are of 5 types.
We are assuming a=8 and b=2.

  1. Addition: ‘ +
    
    a + b
    //Answer: 10
    
  2. Subtraction: ‘
    
    a - b
    //Answer: 6
    
  3. Multiplication: ‘ *
    
    a * b
    //Answer: 16
    
  4. Division: ‘ /
    
    a / b
    //Answer: 4
    
  5. Modulus : ‘ %
    
    a % b
    //Answer: 0 (Because reminder of  8/2 is 0)
    

Note: The division operator gives quotient, while modulus operator gives reminder.

Unary Arithmetic operators

These are of 2 types. These operators can be used before or after the operand.

  1. Increment Operator ++ (Plus-Plus)
  2. It increments the value of given operand by 1.

    
    a = 10;
    a++; //incremented value by 1, a is now 11
    b=10;
    ++b; //incremented value by 1, b is now 11
    
  3. Decrement Operator – – (Minus-Minus)
  4. It decrements the value of given operand by 1

    
    a = 10;
    a--; //decremented value by 1, a is now 9
    b = 10;
    --b; //decremented value by 1, b is now 9
    

The above examples have used the post and pre increment/decrement operators in same way for the basic understanding. However, there is significant difference as well in particular scenarios which shall be discussed later.

Assignment Operators in C

They assign the value to the identifier on the left of operator from literal, another identifier or expression on right of operator.

  1. Assign: ‘ =

    
    a = 10; //assign value 10 to a
    b = a; //Assign value of a to b
    
  2. Perform Operation and Assign: +=   -=   *=   /=

    
    a = 5; //a has value 5
    a += 10; //is same as (a = a + 10) i.e. a = 5 + 10 which assigns value 15 to a
    
    b = 6; //b has value 6
    b -= 2; //is same as (b = b - 2) i.e. b = 6 - 2 which assigns value 4 to b
    

Relational Operators in C

These are also called comparison operators, used to compare the values of two operands i.e. they tell whether the condition is true (1) or false (0).

  1. Less than <
  2. Greater than >
  3. Less than equal to <=
  4. Greater than equal to >=
  5. Equal equal to ==
  6. Not equal to !=

a = 10; //assign value 10 to a
a < 14; //is value of a less than 14 ? True. hence, result is 1
a >= 14; //is value of a greater than or equal to 14 ? False. hence, result is 0
a != 14; //is value of a not equal to 14 ? True. hence, result is 1
a == 14; //is value of a equal 14 ? False. hence, result is 0

Please Note that = is assignment operator, and == is comparison operator which checks whether values are equal or not.

Logical Operators in C

These operators are used to make a decision by result of two or more conditions (relations).

  1. AND & : Both conditions should be satisfied
  2. 
    a = 10;
    a < 14 & a > 8; //a is less than 14 and a is greater than 8, hence result is 1 (true)
    a < 14 & a > 12; //a is less than 14 and a is not greater than 12 , hence result is 0 (false)
    
  3. OR | : at least one condition should be satisfied
  4. 
    b = 10;
    b < 14 | b > 12; //b is less than 14 or greater than 12, hence result is 1 (true)
    b > 14 | b < 8; //neither b is greater than 14, nor a is not less than 8 , hence result is 0 (false)
    
  5. Short Circuit AND &&
  6. Short Circuit OR ||
  7. These operators gives same output as their normal versions but they execute quicker.
    If AND operator finds false condition, it would not check the other condition since result would be false anyway. Similarly, if OR operator finds true condition, it won’t check other conditions.

  8. NOT ! : Reverse the Output
  9. 
    a = 5;
    (! a > 10); //True. a > 10 is false, but not operator is making it true, hence result is 1;
    

Summary

This is a basic introduction about operators and their usage. These are essential elements to get started with coding. In practice, there are several more operators in C programming for specific purposes which are not discussed at this point to keep things simple.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages