# Operators in C

In this article we are going to learn different operators in C, so lets start.

An operator is a symbol that works on the variable or on the values of the variable, which directs the compiler what is to be done. These are used in specific mathematical and logical operations.

## There are different types of operators in C, they are:

• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operator
• Assignment Operator
• Misc. Operators

Let us discuss them one by one.

### Arithmetic Operators:

Such kind of operators performs all kind of mathematical operations, they are:

#### Example illustrating the arithmetic operators:

```#include<stdio.h>
int main()
{
int a=20,b=10;
int res1=a-b;//subtraction operator
printf("\nThe subtraction operator=%d",res1);
int res2=a*b;//multiplication operator
printf("\nThe multiplication operator=%d",res2);
int res3=a/b;//division operator
printf("\nThe division operator=%d",res3);
int res4=a%b;//modulo operator
printf("\nThe modulo operator=%d",res4);
return 0;
}```

#### Output: ### Relational Operator:

As the name suggests a relational operator shows the relation between two operands. The following table shows all the relational operator.

#### An example illustrating the relational operator:

```#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);
return 0;
}```

#### Output: ### Logical Operator:

The operators used to evaluate two or more expressions are called logical operators. The following table shows the operators:

#### An example illustrating the logical operator:

```#include <stdio.h>
int main()
{
int a=10, b=20, c=10, res;

res=(a==b) && (c>b);
printf("(a == b) && (c > b) is %d \n", res);

res=(a==b) && (c<b);
printf("(a == b) && (c < b) is %d \n", res);

res=(a==b) || (c<b);
printf("(a == b) || (c < b) is %d \n", res);

res=(a!=b) || (c<b);
printf("(a != b) || (c < b) is %d \n", res);

res=!(a!=b);
printf("!(a != b) is %d \n", res);

res=!(a==b);
printf("!(a == b) is %d \n", res);

return 0;
}```

#### Output: ### Bitwise Operator:

This operator works bit by bit and performs bit by bit operation. The following table shows the bitwise operators:

#### An example illustrating a bitwise operator:

```//example of bitwise operator

#include<stdio.h>

int main()
{
int a=6,b=4;
printf("(a&b)=>%d\n",a&b);
printf("(a|b)=>%d\n",a|b);
printf("~(a)=>%d\n",~a);
printf("~(b)=>%d\n",~b);
printf("(a>>2)=>%d\n",a>>2);
printf("(a<<2)=>%d\n",a<<2);
printf("(b>>2)=>%d\n",b>>2);
printf("(b<<2)=>%d\n",b<<2);
return 0;
}```

#### Output:

```#Ouutput
(a&b)=>4
(a|b)=>6
~(a)=>-7
~(b)=>-5
(a>>2)=>1
(a<<2)=>24
(b>>2)=>1
(b<<2)=>16```

### Assignment Operator:

This operator is used to assign value to a variable. The table shows the assignment operators:

#### Example illustrating the assignment operator:

```#include <stdio.h>
int main()
{
int a = 5, c;

c = a;
printf("c = %d\n", c);
c += a;
printf("c = %d\n", c);
c -= a;
printf("c = %d\n", c);
c *= a;
printf("c = %d\n", c);
c /= a;
printf("c = %d\n", c);
c %= a;
printf("c = %d\n", c);

return 0;
}```

#### Output: #### Misc. Operators:

This operator contains the other operators such as the increment, decrement and sizeof operators. The table below shows the operators:

#### Example illustrating the misc. operators:

```#include <stdio.h>
int main()
{
int a = 10, a1 = 20;
float b;
double c;
char d;

printf("++a = %d \n", ++a);
printf("--b = %d \n", --a1);
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));

return 0;
}```

#### Output: Here we end our discussion on operators in C, hope u like it and have cleared all your doubts related to this topic. ##### Soham Malakar

Hello my name is Soham Malakar am currently pursuing BCA

Coding is one of my favorite hobbies.
I love to code and an enthusiast to learn different types of programming languages.
I love to get intoxicated in the world of technology and video games.
A quick learner possessing better results.

Articles: 40