# Operators in C

## Operators

The operator is a symbol that tells the compiler to perform any mathematical or logical operation. There are a number of built-in-operators in C programming.

The symbols, however, are used to perform any type of mathematical and logical operation. In C programming, they are called Operators. In the programming, operators are used to manipulate data and variables.

The combination of operands and operators is called expression. Operands are variables that perform some operations in conjunction with the operators.

### Types of operators

Operators are of two types:

• Unary operators
• Binary operators

Unary operator:- These types of operators are used only with one operand.

Binary operator:-These types of operators are used with two operands.

### Arithmetic operator

The arithmetic operators is used to perform mathematical operations like addition, subtraction division and multiplication etc. Below is a list for the type and function of these operators. Assume A=5 and B=10

OperatorFunction performed by operatorExample
+Adds two operands(float, integer etc.)A + B = 15
Subtracts  2nd operand from the 1st one.A − B = -5
*Multiplies the operands.A * B = 50
/Divides numerator by denominator.B / A = 2
%Returns remainder after dividing 1st operand with second one.B % A = 0
++Increment operator increases the integer value by one.++A = 6
Decrement operator decreases the integer value by one.A– = 4

### C program to demonstrate arithmetic operator

```#include<stdio.h>
int main()
{
int a = 5,b = 10,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 = b%a;
printf("b modulus a= %d \n",c);
c=++a;
printf("a++= %d \n",c);
a=5;
c=a--;
printf("a--= %d \n",a);
return 0;
}```

### Output

```a+b = 15
a-b = -5
a*b = 50
a/b = 0
b modulus a= 0
a++= 6
a--= 4
```

### Relational operator

Relational operators are used to compare values of two variables. As you can use these operators, you can find out, whether the values of any two variables are equal or not. If not equal, then the value of which variable is big and which variable’s value is small. Such operators are used with conditional statements. These operators are used to check the condition. when the condition is true, the value becomes true and the values becomes false when the condition is false. All the relational operators that are being used in C language are given below. Assume A=5, B=10

OperatorFunction performed by operatorExample
==Compares if the values of two operands are equal or not. If yes, then the condition becomes true, false otherwise.(A == B) is not true.
!=Compares if the values of two operands are equal or not. If the values are not equal, then the condition becomes true, false otherwise.(A != B) is true.
>Compares if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true, false otherwise.(A > B) is not true.
<Compares if the value of left operand is less than the value of right operand. If yes, then the condition becomes true, false otherwise.(A < B) is true.
>=Compares if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true, false otherwise.(A >= B) is not true.
<=Compares if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true, false otherwise.(A <= B) is true.

### C program to demonstrate use of Relational operator

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

### Output

```a=5, b=10
5 == 10 is 0
5 != 10 is 1
5 > 10 is 0
5 < 10 is 1 5 >= 10 is 0
5 <= 10 is 1```

### Logical operator

The logic gate can be applied (finds application in mathematical operations too) with these operator. Logical operators are used with decision making statements. These operators are used to check two condition together in control statements The table about logical operators is being given. Assume A=5, B=10.

OperatorFunction performed by operatorExample
&&Logical AND operator. If both the conditions are satisfied then the condition becomes true .(A>8 && B==10) is false.
||Logical OR Operator. If any of the conditions are satisfied, then the condition becomes true.(A>8 || B==10) is true.
!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>8 || B==10) is false.

### C program to demonstrate use of logical operator

```#include <stdio.h>
int main()
{
int a = 5, b = 10;
printf("(a>8 && b==10) will return %d",(a>8)&&(b==10));
printf("\n(a>8 || b==10) will return %d",(a>8)||(b==10));
printf("\n!(a>8 || b==10) will return %d",!((a>8)||(b==10)));
return 0;
}```

### Output

```(a>8 && b==10) will return 0
(a>8 || b==10) will return 1
!(a>8 || b==10) will return 0```

### Bitwise operator

The bitwise operators work on bits of a integer value. Bitwise operators are used to perform bit level operations on given variables. The decimal values of the variables are converted to bits. After this the operations are performed on those bits, to understand it lets recall some basics –

pqp & qp | q
0000
0101
1111
1001

Assume A = 50 and B = 10 in binary, they can be written as: A = 0011 0010 B = 0000 1010 Now in the case of A and B we compare 2 bits one from A and the corresponding bit of B And bitwise comparison A&B= 0000 0010 Or bitwise comparison A|B=0011 1010 Assume A=50, B=10

OperatorFunction performed by operatorExample
&Binary AND Operator copies a bit to the result if it exists in both operands else copies zero.(A & B) = 2
|Binary OR Operator copies a bit if it exists in either operand.(A | B) = 58
^Bitwise exclusive OR Operator. If the corresponding bit of any of the operand is 1 then the output would be 1, otherwise 0.(A ^ B) = 56
~Binary compliment operator i.e.- 0 changes to 1 and 1 to 0(~A ) = -51
<<Binary Left shift Operator which rotates the number of bits to the left by specified positions as mentioned on the right.A << 2 = 200
>>Binary Right Rotation Operator which rotates the number of bits to right by specified positions as mentioned on the right.A >> 2 = 12

### C program to demonstrate use of Bitwise operator

```#include <stdio.h>
int main()
{
int a =50, b =10;
printf("a&b = %d", a&b);
printf("\na|b = %d", a|b);
printf("\na^b = %d", a^b);
printf("\n~a = %d", ~a);
printf("\na<<2 = %d",a<<2); printf("\na>>2 = %d", a>>2);
return 0;
}```

### Output

```a&b = 2
a|b = 58
a^b = 56
~a = -51
a<<2 = 200 a>>2 = 12```

### Assignment operator

Assignment operators are used to assign values of variables to each other. The operator which changes the value of the operands themselves, here is how they work

OperatorFunction performed by operatorExample
=Simple assignment operator. Assigns values from right side operands to left side operandC = A + B will assign the value of A + B to C
+=Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.C += A is equivalent to C = C + A
-=Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.C -= A is equivalent to C = C – A
*=Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.C *= A is equivalent to C = C * A
/=Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.C /= A is equivalent to C = C / A
%=Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.C %= A is equivalent to C = C % A
<<=Left shift AND assignment operator.C <<= 2 is same as C = C << 2
>>=Right shift AND assignment operator.C >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operator.C &= 2 is same as C = C & 2
^=Bitwise exclusive OR and assignment operator.C ^= 2 is same as C = C ^ 2
|=Bitwise inclusive OR and assignment operator.C |= 2 is same as C = C | 2

### C Program to demonstrate use of assignment operator

```#include <stdio.h>
int main()
{
int a = 10, c;
c = a;      // c is 5
printf("c = %d\n", c);
c += a;     // c is 10
printf("c = %d\n", c);
c -= a;     // c is 5
printf("c = %d\n", c);
c *= a;     // c is 25
printf("c = %d\n", c);
c /= a;     // c is 5
printf("c = %d\n", c);
c %= a;     // c = 0
printf("c = %d\n", c);
return 0;
}
```

### Output

```c = 10
c = 20
c = 10
c = 100
c = 10
c = 0```

### Misc operator

There are a few more miscellaneous operator worth mentioning

OperatorFunction performed by operatorExample
sizeof()Returns the size of a variable.sizeof(a), for an integer value, it returns 4.
&Returns the address of a variable.&a; returns the address of the memory location of variable.
*Pointer to a variable.*a;
? :Conditional Expression.If Condition is true ? then value X : otherwise value Y