An operator is a symbol used to perform specific mathematical or logical functions. C language has multiple built-in operators.

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Other Operators

Below table shows all the arithmetic operators supported in **C** language.

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

+ | Adds two operands. |

− | Subtracts second operand from the first. |

* | Multiplies both operands. |

/ | Divides numerator by de-numerator. |

% | Modulus Operator and remainder of after an integer division. |

++ | Increment operator increases the integer value by one. |

-- | Decrement operator decreases the integer value by one. |

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

== | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. |

!= | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. |

> | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. |

< | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. |

>= | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. |

<= | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. |

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

&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. |

|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. |

! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. |

A | B | A & B | A | B | A ^ B |
---|---|---|---|---|

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

1 | 0 | 0 | 1 | 1 |

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

= | P = M + N will assign the value of M + N to P |

+= | P += Q is equivalent to P = P + Q |

-= | P -= Q is equivalent to P = P - Q |

*= | C *= A is equivalent to C = C * A |

/= | C /= A is equivalent to C = C / A |

%= | C %= A is equivalent to C = C % A |

<<= | C <<= 2 is same as C = C << 2 |

>>= | C >>= 2 is same as C = C >> 2 |

&= | C &= 2 is same as C = C & 2 |

^= | C ^= 2 is same as C = C ^ 2 |

|= | C |= 2 is same as C = C | 2 |

Returns the size of a variable.

Example -

```
char a;
sizeof(a) //it will return 1 (memory size of character).
```

Conditional Expression : If Condition is true ? then value X : otherwise value Y

Example-

```
int x = (y == 2) ? 5 : 6;
//if y is 2 then x will get 5 otherwise 6
```

Operator precedence determines how an expression is evaluated i.e. in which operator will be evaluated. Higher precedence operator will be evaluated first and then next one.

For example, sum = 8 + 4 * 6; In this expression since * has higher precedence than + operator. sum will be 32.

Category | Operator | Associativity |
---|---|---|

Postfix | () [] -> . ++ - - | Left to right |

Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |

Multiplicative | * / % | Left to right |

Additive | + - | Left to right |

Shift | << >> | Left to right |

Relational | < <= > >= | Left to right |

Equality | == != | Left to right |

Bitwise AND | & | Left to right |

Bitwise XOR | ^ | Left to right |

Bitwise OR | | | Left to right |

Logical AND | && | Left to right |

Logical OR | || | Left to right |

Conditional | ?: | Right to left |

Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |

Comma | , | Left to right |

© 2018 BreakInerview