## Operators in C

There are various operators in C for performing various mathematical and logical functions.

Operator operates on operand.

**For example:** +(plus) is an operator that performs addition.

C language provides various types of operators such as:

**1.**Arithmetic Operator**2.**Relational Operator**3.**Logical Operator**4.**Bitwise Operator**5.**Assignment Operator**6.**Miscellaneous/other Operator

**1. Arithmetic operator:** Arithmetic operators are +, -, * and / . These operator works same, as they work in simple math’s. These operator can be used by any built in data type.

The modulus(%) operator finds the remainder of an integer division. *Modulus operator cant be used on floating point types.*

**Example :**

```
#include <stdio.h>
```
int main()
{
int a = 7,b = 4, c;
c = a+b;
printf(“a+b = %d \n”,c);
c = a-b;
printf(“a-b = %d \n”,c);
c = a*b;
printf(“a*b = %d \n”,c);
c=a/b;
printf(“a/b = %d \n”,c);
c=a%b;
printf(“Remainder when a/b = %d \n”,c);
return 0;
}

**Output :**

a+b = 11 a-b = 3 a*b = 28 a/b = 1 Remainder when a/b=3

**– Increment and decrement operator:** As the name suggests these operators are used to do increment or decrement to its operand.

Increment operator(++) adds 1 to its operand. And The decrement(–) operator subtracts 1 from its operand.

**Example :**

```
#include <stdio.h>
int main()
{
int a = 10, b = 20;
printf(“++a = %d \n”, ++a);
printf(“--b = %d \n”, --b);
return 0;
}
```

**Output :**

++a = 11 --b = 19

**2. Relational operator:** Relational operators are the operator that shows some relation between the values.

Meaning

Operator | |
---|---|

> | Greater than |

>= | Greater than or Equal to |

< | Less than |

<= | Less than or Equal to |

== | Equal |

!= | Not Equal |

**Example :**

```
#include <stdio.h>
int main()
{
int a=4,b=2;
if (a == b)
{
printf(“a and b are equal”);
}
else
{
printf(“a and b are not equal”);
}
return 0;
}
```

**Output :**

a and b are not equal

**3. Logical operator:** Logical operators are used to perform logical operations between two operands. An expression that contains logical operator return 0 or 1 depending on the result of operation True or False.

Meaning

Operator | |
---|---|

&& | AND |

|| | OR |

! | NOT |

**4. Bitwise operator:** Bitwise operator is used to perform bitwise operations on the operand. Before performing the calculations the operators are converted into bit level. This makes the processing faster and also saves power.

MeaningBitwise AND|Bitwise OR

Operator | |
---|---|

& | |

^ | Bitwise exclusive OR |

~ | Bitwise complement |

<< | Shift left |

>> | Shift right |

**5. Assignment operator:** Assignment operator is used to assign a value to a variable.

The assignment operators supported by C language are:

Operator | Example | can be written as |
---|---|---|

= | a=b | a=b |

+= | a+=b | a=a+b |

-= | a-=b | a=a-b |

*= | a*=b | a=a*b |

/= | a/=b | a=a/b |

%= | a%=b | a=a%b |

**Example :**

```
#include <stdio.h>
int main()
{
int a = 5, b;
b = a;
printf(“b = %d \n”, b);
b += a;
printf(“b = %d \n”, b);
b -= a;
printf(“b = %d \n”, b);
b *= a;
printf(“b = %d \n”, b);
b /= a;
printf(“b = %d \n”, b);
b %= a;
printf(“b = %d \”, b);
return 0;
}
```

**Output :**

b = 5 b = 10 b = 5 b = 25 b = 5 b = 0

**6. Miscellaneous/other:**

**– sizeof operator:**

sizeof operator is a unary operator and returns the size of operand.

**Example :**

```
#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf(“size of int data type:%d \n”,sizeof(a));
printf(“size of char data type:%d \n”,sizeof(b));
printf(“size of float data type:%d \n”,sizeof(c));
printf(“size of double data type:%d\n”,sizeof(d));
return 0;
}
```

**Output :**

size of int data type:4 size of char data type:1 size of float data type:4 size of double data type:8

Report Error/ Suggestion