# C Operator

Advertisements

## Operator in C

An operator is a symbol that tells the compiler to perform specific mathematical or logical calculations on operands(variables).

### Types of operators available in C

• Assignment operator
• Arithmetic / Mathmetical operator
• Increment Decrement operator
• Relational operator
• Logical operator
• Conditional or Ternary operator
• Binary operator
• Unary operator

## Assignment operator

Assignment operator is used to copy value from right to left variable.

```		Suppose we have,

float X = 5, Y = 2;
```
 Operator Name Description Example = Equal sign Copy value from right to left. X = Y, Now both X and Y have 5 += Plus Equal to Plus Equal to operator will return the addition of right operand and left operand. X += Y is similar to X = X + Y, now X is 7 -= Minus Equal to Minus Equal to operator will return the subtraction of right operand from left operand. X -= Y is similar to X = X - Y, now X is 3 *= Multiply Equal to Multiply Equal to operator will return the product of right operand and left operand. X *= Y is similar to X = X * Y, now X is 10 /= Division Equal to Division Equal to operator will divide right operand by left operand and return the quotient. X /= Y is similar to X = X / Y, now X is 2.5 %= Modulus or Mod Equal to Modulus Equal to operator will divide right operand by left operand and return the mod ( Remainder ). X %= Y is similar to X = X % Y, now X is 1

## Arithmetic operator

Arithemtic operators are used for mathematical operations.

```		Suppose we have,

int X = 5, Y = 2;
```
 Operator Name Description Example + Plus Return the addition of left and right operands. (X + Y) will return 7 - Minus Return the difference b/w right operand from left operand. (X - Y) will return 3 * Multiply Return the product of left and right operands. (X * Y) will return 10 / Division Return the Quetiont from left operand by right operand. (X / Y) will return 2(both are int, int doesn't support decimal) % Modulus or Mod Return the Modulus ( Remainder ) from left operand by right operand. (X % Y) will return 1

## Relational Operators

Relational operators are used for checking conditions whether the given condition is true or false. If the condition is true, it will return non-zero value, if the condition is false, it will return 0.

```		Suppose we have,

int X = 5, Y = 2;
```
 Operator Name Description Example > Greater then Check whether the left operand is greater then right operand or not. (X > Y) will return true < Smaller then Check whether the left operand is smaller then right operand or not. (X < Y) will return false >= Greater then or Equal to Check whether the left operand is greater or equal to right operand or not. (X >= Y) will return true <= Smaller then or Equal to Check whether the left operand is smaller or equal to right operand or not. (X <= Y) will return false == Equal to Check whether the both operands are equal or not. (X == Y) will return false != Not Equal to Check whether the both operands are equal or not. (X != Y) will return true

## Logical Operators

Logical operators are used in situation when we have more then one condition in a single if statement.

```		Suppose we have,

int X = 5, Y = 2;
```
 Operator Name Description Example && AND Return true if all conditions are true, return false if any of the condition is false. if(X > Y && Y < X) will return true || OR Return false if all conditions are false, return true if any of the condition is true. if(X > Y || X < Y) will return true ! NOT Return true if condition if false, return false if condition is true. if(!(X>y)) will return false

## Conditional Operator ?:

The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. First is condition, second and third is value. The conditional operator check the condition, if condition is true, it will return second value, if condition is false, it will return third value.

### Syntax :

`		val = condition ? val1 : val2;`

### Example :

```
void main()
{
int X=5,Y=2,lrg;

lrg = (X>Y) ? X : Y;

printf("\nLargest number is : %d",lrg);
}

Output :

Largest number is : 5

```

## Binary operator

Binary operators are those operators that works with at least two operands such as (Arithmetic operators) +, -, *, /, %.

## Unary operator

Unary operators are those operators that works with singal operands such as (Increment or Decrement operators) ++ and --.

Advertisement