## OPERATORS IN C Operators are the base of any programming language. Thus the functionality of the programming language is incomplete without them. We can see an operator as a symbol that helps us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands.

for example, x = 2+3

in this example, 2,3 is the operands, and ‘+’ is the operator.

C language offers many types of operators. They are,

1 Arithmetic operator

2. Assignment operator

3. Relational operator

4. Logical operator

5. Bitwise operator

6. Conditional operator (ternary operator)

7 .Increment/decrement operator

8 . Special operator

### 1. ARITHMETIC OPERATORS IN C:

C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus in C programs

#### EXAMPLE PROGRAM FOR C ARITHMETIC OPERATOR:

In this example program, two values ―40‖ and ―20‖ are used to perform arithmetic operations such as addition, subtraction, multiplication, division, modulus, and output is displayed for each operation

```#include <stdio.h>
int main()
{
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}

```

### 2. ASSIGNMENT OPERATOR IN C:

In C programs, values for the variables are assigned using assignment operators. For example, if the value ―10‖ is to be assigned for the variable ―sum‖, it can be assigned as
―sum = 10;‖
Other assignment operators in the C language are given below.

#### EXAMPLE PROGRAM FOR C ASSIGNMENT OPERATOR:

In this program, values from 0 – 9 are summed up and a total ―45‖ is displayed as output. Assignment operators such as ―=‖ and ―+=‖ are used in this program to assign the values and to sum up the values.

```# include <stdio.h>
int main()
{
int Total=0,i;
for(i=0;i<10;i++)
{
Total+=i; // This is same as Total = Toatal+i
}
printf("Total = %d", Total);
}

```

### 3. RELATIONAL OPERATOR IN C:

Relational operators are used to finding the relation between two variables. i.e. to compare the values of two variables in a C program.

EXAMPLE PROGRAM FOR RELATIONAL OPERATORS IN C
In this program, the relational operator (==) is used to compare 2 values whether they are equal are not. If both values are equal, the output is displayed as ‖ values are equal‖ to. Else, the output is displayed as ―values are not equal‖.
Note: double equal sign (==) should be used to compare 2 values. We should not single equal sign (=)

```#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
```

### 4. LOGICAL OPERATOR IN C:

These operators are used to perform logical operations on the given expressions. There are 3 logical operators in the C language. They are, logical AND (&&), logical OR (||), and logical NOT (!).

```#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are true\n");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is true\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}
}
```

&& operator: – if clause‖ becomes true only when both conditions (m>n and m! =0) are true. Else, it becomes false.

|| Operator:– if clause‖ becomes true when any one of the conditions (o>p || p!=20) is true. It becomes false when none of the conditions is true. ! Operator – It is used to reverses the state of the operand.

If the conditions (m>n && m!=0) is true, true (1) is returned. This value is inverted by the ―!‖ operator. So, ―! (m>n and m! =0)‖ returns false

### 5. BITWISE OPERATOR IN C

These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit-wise operators work on these bits. Bitwise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR), << (left shift) and >> (right shift)

Note:

Bitwise NOT:

Value of 40 in binary is0000000000000000000000000000000000000000000000000010100000000000. So, all 0‘s are converted into 1‘s in bit wise NOT operation.

Bitwise left shift and right shift :

In left shift operation ―x << 1 ―, 1 means that the bits will be left-shifted by one place. If we use it as ―x << 2 ―, then, it means that the bits will be left-shifted by 2 places. EXAMPLE PROGRAM FOR BIT WISE OPERATORS IN C In this example program, bit-wise operations are performed as shown above and output is displayed in decimal format.

### 6. CONDITIONAL OR TERNARY OPERATORS IN C:

Conditional operators return one value if the condition is true and returns another value if a condition is false. This operator is also called a ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
In the above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if-else conditional statements.

```#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
```

### 7. C – Increment/decrement Operator:

PREVNEXT
Increment operators are used to increasing the value of the variable by one and decrement operators are used to decreasing the value of the variable by one in C programs.
Syntax:
Increment operator: ++var_name ;( or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;

```#include <stdio.h>
int main()
{
int i=1;
while(i<10)
{
printf("%d ",i);
i++;
}
}
```

### 8. SPECIAL OPERATORS IN C:

EXAMPLE PROGRAM FOR & AND * OPERATORS IN C
In this program, the ―& symbol is used to get the address of the variable, and the ―*‖ symbol is used to get the value of the variable that the pointer is pointing to. Please refer to C – pointer topic to know more about pointers

```#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
```