## C Operators

## Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc Operators

## Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

Operator | Description | Example |
---|---|---|

+ | Adds two operands. | A + B = 30 |

− | Subtracts second operand from the first. | A − B = -10 |

* | Multiplies both operands. | A * B = 200 |

/ | Divides numerator by de-numerator. | B / A = 2 |

% | Modulus Operator and remainder of after an integer division. | B % A = 0 |

++ | Increment operator increases the integer value by one. | A++ = 11 |

-- | Decrement operator decreases the integer value by one. | A-- = 9 |

#### a) Unary Operators:

Operators that operate or work with a single operand are unary operators.

For example: Increment(++) and Decrement(–) Operators

#### b) Binary Operators:

Operators that operate or work with two operands are binary operators.

For example: Addition(+), Subtraction(-), multiplication(*), Division(/) operators

## Relational Operators:

These are used for the comparison of the values of two operands.

For example, checking if one operand is equal to the other operand or not, whether an operand is greater than the other operand or not, etc. Some of the relational operators are (==, >= , <= ).

## Logical Operators

Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false. Logical operators are used to determine the logic between variables or values:

Operator | Name | Description |
---|---|---|

&& | Logical and | Returns true if both statements are true |

|| | Logical or | Returns true if one of the statements is true |

! | Logical not | Reverse the result, returns false if the result is true |

## Bitwise Operators:

The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands.

Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit-level for faster processing.

For example, the bitwise AND represented as & operator in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

## Assignment Operators

Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. Assignment operators are used to assign values to variables.

Operator | Description | Example |
---|---|---|

= | Simple assignment operator. Assigns values from right side operands to left side operand | C = A + B will assign the value of A + B to C |

+= | Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. | C += A is equivalent to C = C + A |

-= | Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C - A |

*= | Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A is equivalent to C = C * A |

/= | Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. | C /= A is equivalent to C = C / A |

%= | Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. | C %= A is equivalent to C = C % A |

<<= | Left shift AND assignment operator. | C <<= 2 is same as C = C << 2 |

>>= | Right shift AND assignment operator. | C >>= 2 is same as C = C >> 2 |

&= | Bitwise AND assignment operator. | C &= 2 is same as C = C & 2 |

^= | Bitwise exclusive OR and assignment operator. | C ^= 2 is same as C = C ^ 2 |

|= | Bitwise inclusive OR and assignment operator. | C |= 2 is same as C = C | 2 |

## Miscellaneous Operators in C

There are some few others important operators in C programming language. i,e. sizeof, &, *, ?

Operator | Description | Example |
---|---|---|

sizeof() | Returns the size of a variable. | sizeof(a), where a is integer, will return 4. |

& | Returns the address of a variable. | &i; returns the actual address of the variable. |

* | Pointer to a variable. | *i; indicate a pointer variable i. |

? : | Conditional expression. | i==1?”Y”:”N”; If condition is true then value Y otherwise value N. |

## Comparison Operators

Comparison operators are used to compare two values.

**Note:**The return value of a comparison is either true (1) or false (0).

In the following example, we use the **greater than** operator (>) to find out if 5 is greater than 3:

A list of all comparison operators:

Operator | Name | Example |
---|---|---|

== | Equal to | x == y |

!= | Not equal | x != y |

> | Greater than | x > y |

< | Less than | x < y |

>= | Greater than or equal to | x >= y |

<= | Less than or equal to | x <= y |