Introduction

Fundamentals

C Flow Control

C Functions

C Arrays

# Operators in C Language

An operator is a symbol that performs an operation on a value or variable. In this tutorial, we will learn about various C operators like arithmetic, increment, assignment, relational, logical and bitwise, with the help of examples.

An operator is a symbol which indicates compiler to perform a specific mathematical or logical operation on some values. C language is rich in built-in operators and provides the following types of operators:

### 1. C Arithmetic Operators:

Arithmetic operators are commonly used to perform various mathematical operations between two values. Below table indicates the arithmatic operators available in C Language:

Operator | Name | Description | Example |
---|---|---|---|

+ | Addition | Adds two values together | a + b |

- | Subtraction | Subtracts one value from another | a - b |

* | Multiplication | Multiplies two values | a * b |

/ | Division | Divides one value by another | a / b |

% | Modulus | Returns the division remainder | a % b |

##### Example 1: Arithmetic Operators

```
//Options with the help of arithmatic operators
#include <stdio.h>
int main()
{
int num1 = 9,num2 = 4, num3;
num3 = num1+num2;
printf("num1 + num2 = %d \n",num3);
num3 = num1-num2;
printf("num1 - num2 = %d \n",num3);
num3 = num1*num2;
printf("num1 * num2 = %d \n",num3);
num3 = num1/num2;
printf("num1 / num2 = %d \n",num3);
num3 = num1%num2;
printf("Remainder when num1 divided by num2 = %d \n",num3);
return 0;
}
```

Output :

num1 + num2 = 13 num1 - num2 = 5 num1 * num2 = 36 num1 / num2 = 2 Remainder when num1 divided by num2 = 1

As you might have predicted, the operators +, -, and * compute addition, subtraction, and multiplication, respectively.
In basic math, **9/4 = 2.25**. However, the output of the program is **2**.

It is because **num1** and **num2** are both integer variables. The output is therefore also an integer. The compiler displays result **2** rather than **2.25** because it ignores the terms after the decimal point.

The remainder is calculated using the modulo operator **%**. The remainder is **1** when **num1 = 9** is divided by **num2 = 4**. Only integers can be used with the **%** operator.

### 2. Increment and Decrement Operators in C:

##### (i). Increment Operator

The increment operator is used to increase the value of a variable. The value is first increased and then used inside the expression in the Pre-Increment. In the Post-Increment, the value is first used within the expression before being increased.

**Syntax:**

```
// PREFIX
++num
// POSTFIX
num++
where num is a variable
```

##### Example 2: Increment Operator

```
#include<stdio.h>
int main()
{
int x = 10 ;
int a ;
x = ++x ;
printf("Value of x = %d \n", x);
a = x++ ;
printf("Value of a = %d \n", a);
printf("New Value of x = %d", a);
return 0;
}
```

Output :

Value of x = 11 Value of a = 11 New Value of x = 11

##### (ii). Decrement Operator

To decrease the value of a variable in an expression, use the decrement operator. The value is decremented first in the Pre-Decrement and then used inside the expression. In the Post-Decrement, the value is first used within the expression before being decremented.

**Syntax:**

```
// PREFIX
--num
// POSTFIX
num--
where num is a variable
```

##### Example 3: Decrement Operator

```
#include<stdio.h>
int main()
{
int x = 10 ;
int a ;
x = --x ;
printf("Value of x = %d \n", x);
a = x-- ;
printf("Value of a = %d \n", a);
printf("New Value of x = %d", a);
return 0;
}
```

Output :

Value of x = 9 Value of a = 9 New Value of x = 9

### 3. Assignment Operators in C:

An assignment operator is used for assigning a particular value to a variable. The most common assignment operator is =.

Operator | Example | Same As |
---|---|---|

= | a = 5 | a = 5 |

+= | a += 2 | a = a + 2 |

-= | a -= 2 | a = a - 2 |

*= | a *= 2 | a = a * 2 |

/= | a /= 2 | a = a / 2 |

%= | a %= 2 | a = a % 2 |

##### Example 4: Assignment Operators

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

Output :

c = 5 c = 10 c = 5 c = 25 c = 5 c = 0

### 4. Relational Operators in C:

The relationship between two operands is checked by a relational operator. It returns 1 if the relation is true and 0 if the relation is false.

Operator | Name | Example |
---|---|---|

== | Equal to | 4 == 3 is evaluated to 0 |

> | Greater than | 4 > 3 is evaluated to 1 |

< | Less than | 4 < 3 is evaluated to 0 |

!= | Not equal | 4 != 3 is evaluated to 1 |

>= | Greater than or equal to | 4 >= 3 is evaluated to 1 |

<= | Less than or equal to | 4 <= 3 is evaluated to 0 |

##### Example 5: Relational Operators

```
#include <stdio.h>
int main()
{
int a = 10, b = 10, c = 20;
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 :

10 == 10 is 1 10 == 20 is 0 10 > 10 is 0 10 > 20 is 0 10 < 10 is 0 10 < 20 is 1 10 != 10 is 0 10 != 20 is 1 10 >= 10 is 1 10 >= 20 is 0 10 <= 10 is 1 10 <= 20 is 1

### 5. Logical Operators in C:

A logical operator expression returns either 0 or 1, depending on whether the expression is true or false. In C programming, logical operators are frequently used in decision making.

| Operator | Name | Example | | -------- | ----------- | ------------------------------------------------------- | --- | ------------------- | | && | Logical And | c=10 && d =2,expression ((c==10) && (d>5)) equals to 0. | | || | Logical Or | If c = 7 and d = 1 then, expression ((c==7) | | (d>5)) equals to 1. | | ! | Logical Not | If x = 5 then, expression !(x==5) equals to 0. |

##### Example 6: Logical Operators

```
#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");
}
}
```

Output :

&& Operator: Both conditions are true || Operator: Only one condition is true ! Operator: Both conditions are true.But, status is inverted as false

### 6. Bitwise Operators in C:

Mathematical operations like as addition, subtraction, multiplication, and division are converted to bit-level during computing, which speeds up processing and saves power.

Operator | Meaning of operators |
---|---|

& | Bitwise AND |

| | Bitwise OR |

^ | Bitwise exclusive OR |

~ | Bitwise complement |

<< | Shift left |

>> | Shift right |