# What are the different operators available in C ?

In the C programming language, operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. Operators are used to manipulate variables and constants, and they are essential for building expressions and controlling the flow of a program. Here + is acting as a operator and X and Y as operands.

C has a wide range of operators, which can be classified into the following categories:

**Arithmetic Operators****Assignment Operators****Relational Operators****Logical Operators****Conditional/ Ternary Operator****Bitwise Operators**

## Arithmetic Operators

These operators perform basic arithmetic operations, such as addition, subtraction, multiplication, and division. For example:

Operator |
Description |
Example |

+ | Adds two operands. | A + B = 30 |

− | Subtracts second operand from the first. | A − B = -10 |

* | Multiplies both operands. | A * B = 200 |

/ | Divides numerator by de-numerator. | B / A = 2 |

% | Modulus Operator and remainder of after an integer division. | B % A = 0 |

++ | Increment operator increases the integer value by one. | A++ = 11 |

-- | Decrement operator decreases the integer value by one. | A-- = 9 |

For example:

```
int a = 10;
int b = 20;
int c = a + b; // c will be 30
int d = a * b; // d will be 200
```

## Assignment Operators

These operators assign a value to a variable. For example:**Assume A = 5 and B= 15**

Operator |
Description |
Example |

= | Simple assignment operator. Assigns values from right side operands to left side operand | C = 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 |

For example:

```
int a = 10;
int b = 20;
a = b; // a will be 20
```

## Relational Operators

These operators compare two operands and return a boolean value (`true`

or `false`

) based on the result of the comparison. For example: **Assume A = 5 and B= 15**

Operator |
Description |
Example |

== | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. | (A == B) is not true. |

!= | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. | (A != B) is true. |

> | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. | (A > B) is not true. |

< | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. | (A < B) is true. |

>= | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. | (A >= B) is not true. |

<= | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. | (A <= B) is true. |

For example:

```
int a = 10;
int b = 20;
if (a > b)
{
printf("a is greater than b");
}
else
{
printf("a is not greater than b");
}
```

## Logical Operators

These operators perform logical operations, such as AND, OR, and NOT, on two boolean operands. For example: **A=1 and B =0**

Operator |
Description |
Example |

&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |

|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) is true. |

! | Called 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 && B) is true. |

For example:

```
int a = 10;
int b = 20;
int c = 30;
if (a > b && a < c)
{
printf("a is greater than b and less than c
");
}
else
{
printf("a is not in the range of b and c
");
}
```

## Conditional / Ternary Operator

This operator is also known as the ternary operator, and it takes three operands. It evaluates a condition and returns one of the two operands based on the result of the evaluation. For example:

For Example:

```
int a = 10;
int b = 20;
int min = (a < b) ? a : b; // min will be 10
```

## Bitwise Operators

bitwise operators are special operators that perform bit-level operations on integer operands. These operators are used to manipulate individual bits in a value stored in an integer variable.

Here is a list of bitwise operators in C:

`&`

(bitwise AND): Performs a bit-level AND operation on two operands.`|`

(bitwise OR): Performs a bit-level OR operation on two operands.`^`

(bitwise XOR): Performs a bit-level XOR operation on two operands.`~`

(bitwise NOT): Performs a bit-level NOT operation on a single operand.`<<`

(left shift): Shifts the bits of a number to the left by a specified number of positions.`>>`

(right shift): Shifts the bits of a number to the right by a specified number of positions.

The truth tables for &, |, and ^ is as follows −

a |
b |
a&b |
a | b |
a ^ b |

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

1 | 0 | 0 | 1 | 1 |

**Assume a = 60 and b = 13 in binary format, they will be as follows −**

a = 0011 1100

b = 0000 1101

**Result after Bitwise Operations**

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

## Differentiate between Binary and Unary Operators

**Unary operators**: These operators operate on a single operand. For example:

```
int a = 10;
int b = -a; // b will be -10
int c = ++a; // c will be 11, and a will be 11
int d = --a; // d will be 10, and a will be 10
```

**Binary operators**: These operators operate on two operands. For example:

```
int a = 10;
int b = 20;
int c = a + b; // c will be 30
int d = a * b; // d will be 200
```

## Operator Precedence in C

In C, operator precedence determines the order in which operations are performed in an expression. Operators with higher precedence are evaluated before operators with lower precedence. For example, in the expression `a + b * c`

, the multiplication operator (`*`

) has higher precedence than the addition operator (`+`

), so `b * c`

is evaluated first, and then the result is added to `a`

.

## Operator Associativity in C

Operator associativity determines the order in which operations of the same precedence are performed in an expression. In C, the associativity of an operator is either left-to-right or right-to-left.

Left-to-right associativity means that operations are performed from left to right. For example, in the expression `a - b - c`

, the subtraction operator (`-`

) has left-to-right associativity, so the expression is evaluated as `(a - b) - c`

.

Right-to-left associativity means that operations are performed from right to left. For example, the assignment operator (`=`

) has right-to-left associativity, so the expression `a = b = c`

is evaluated as `a = (b = c)`

.

Here is a list of the operators in C, ordered by precedence, with the highest precedence at the top:

Category |
Operator |
Associativity |

Postfix | () [] -> . ++ - - | Left to right |

Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |

Multiplicative | * / % | Left to right |

Additive | + - | Left to right |

Shift | << >> | Left to right |

Relational | < <= > >= | Left to right |

Equality | == != | Left to right |

Bitwise AND | & | Left to right |

Bitwise XOR | ^ | Left to right |

Bitwise OR | | | Left to right |

Logical AND | && | Left to right |

Logical OR | || | Left to right |

Conditional | ?: | Right to left |

Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |

Comma | , | Left to right |