Operators in C

In this tutorial, we will learn about Operators in C programming language. Operators are special symbols that we use to perform special operations such as the addition of two numbers.

C provides us with a wide range of operators which help us to perform special operations. In C, all the operators are divided into different categories

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

Arithmetic Operators:

Arithmetic operators in C are used are used to perform simple mathematical operations like Addition, subtraction, multiplication, division etc.

Example Program:

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

printf("Subtraction = %d\n",a-b);
printf("Multiplication = %d\n",a*b);
printf("Division = %d\n",a/b);
printf("Modulous = %d\n",a%b);

return 0;
}```

Output:

``````Addition = 26
Subtraction = 16
Multiplication = 105
Division = 4
Modulous = 1  ``````

Relational Operators:

Relational operators are used to checking the relation between two operands such as greater than, less than, equals to etc. If the relation between two operands is true then the result of the relational expression will `1`, if the relation is false then the result of the relational expression will `0`.

Example Program:

```#include <stdio.h>
int main()
{

printf("Greater than = %d\n",10>5);
printf("Greater than equals to= %d\n",10>=15);
printf("Less than = %d\n",5<2);
printf("Less than equals to = %d\n",5<=10);
printf("Equals to = %d\n",20==10);
printf("Not Equal to = %d\n",20!=10);

return 0;
}```

Output:

``````Greater than = 1
Greater than equals to= 0
Less than = 0
Less than equals to = 1
Equals to = 0
Not Equal to = 1``````

Logical Operators:

Generally, logical operators are used with relation expressions but we can also use logical operators with constants. If the result of the logical operator is true then `1` will return otherwise `0` will return.

Example Program:

```#include <stdio.h>
int main()
{
printf("Result of 0&&0 = %d\n",0&&0);
printf("Result of 1&&0 = %d\n",1&&0);
printf("Result of 0&&1 = %d\n",0&&1);
printf("Result of 1&&1 = %d\n",1&&1);
printf("Result of 0||0 = %d\n",0||0);
printf("Result of 1||0 = %d\n",1||0);
printf("Result of 0||1 = %d\n",0||1);
printf("Result of 1||1 = %d\n",1||1);
printf("Result of !1 = %d\n",!1);
printf("Result of !0 = %d\n",!0);

return 0;
}```

Output:

``````Result of 0&&0 = 0
Result of 1&&0 = 0
Result of 0&&1 = 0
Result of 1&&1 = 1
Result of 0||0 = 0
Result of 1||0 = 1
Result of 0||1 = 1
Result of 1||1 = 1
Result of !1 = 0
Result of !0 = 1 ``````

Bitwise Operators:

The Bitwise operators are used to perform operations on the bit level of operands. There is a total of six bitwise operators available in C language.

Example Program:

```#include <stdio.h>
int main()
{

printf("Result of 10&5 = %d\n",10&5);
printf("Result of 10|5 = %d\n",10|5);
printf("Result of 10^5 = %d\n",10^5);
printf("Result of ~10 = %d\n",~10);
printf("Result of 10<<2 = %d\n",10<<2);
printf("Result of 10>>2 = %d\n",10>>2);

return 0;
}```

Output:

``````Result of 10&5 = 0
Result of 10|5 = 15
Result of 10^5 = 15
Result of ~10 = -11
Result of 10<<2 = 40
Result of 10>>2 = 2 ``````

Assignment Operators:

Assignments are used to assign some value to variable. The most common use assignment operator is =.

Example Program:

```#include <stdio.h>
int main()
{
int a=0;
printf("Simple assignment = %d\n",a);
a+=10;
a=10;
a-=2;
printf("Subtract Assignment = %d\n",a);
a=10;
a*=2;
printf("Multiply Assignment = %d\n",a);
a=10;
a/=2;
printf("Divide Assignment = %d\n",a);
a=10;
a%=2;
printf("Modulous Assignment = %d\n",a);
a=10;
a<<=2;
printf("Bitwise Left Sift Assignment = %d\n",a);
a=10;
a>>=2;
printf("Bitwise Right Sift Assignment = %d\n",a);
a=10;
a&=2;
printf("Bitwise AND Assignment = %d\n",a);
a=10;
a|=2;
printf("Bitwise OR Assignment = %d\n",a);
a=10;
a^=2;
printf("Bitwise XOR Assignment = %d\n",a);
return 0;
}```

Output:

``````Simple assignment = 0
Subtract Assignment = 8
Multiply Assignment = 20
Divide Assignment = 5
Modulous Assignment = 0
Bitwise Left Sift Assignment = 40
Bitwise Right Sift Assignment = 2
Bitwise AND Assignment = 2
Bitwise OR Assignment = 10
Bitwise XOR Assignment = 8   ``````

Misc Operators:

There are some miscellaneous operators in C language. Check out the following list.