Operators in C With Examples

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

C-Operators

 

C Operators

Arithmetic Operators in C

Binary Arithmetic operators

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

  1. Addition: ‘ +
  2. Subtraction: ‘
  3. Multiplication: ‘ *
  4. Division: ‘ /
  5. Modulus : ‘ %

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)

It increments the value of given operand by 1.

    1. Decrement Operator – – (Minus-Minus)

It decrements the value of given operand by 1

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: ‘ =
  2. Perform Operation and Assign: +=   -=   *=   /=

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 !=

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

    1. OR | : at least one condition should be satisfied

    1. Short Circuit AND &&
    2. Short Circuit OR ||

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.

    1. NOT ! : Reverse the Output

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.

By admin

Leave a Reply

%d bloggers like this: