## Operators in C Language

‘Welcome in Study Learner‘

Today i will teach you about different opeartors in C programming with the help of examples.

An operator is a symbol that tails the compiler to perform specific logical or mathematical functions. C language provides the following types of operators:-

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Increment/Decrement Operators
- Conditional Operators
- Special Operators

### Arithmetic Operators :-

Arithmetic operators will used to perform basic mathematical operation like Addition(+), Substraction(-), Multiplication(*), Division(/) and Modulus(%).

The following table show all the arithmetic operators with example.

Assume variable A holds 30 and variable B holds 60 then:-

Operator Type | Operator | Operation | Example |

Addition | + | Add two operands. | A + B = 90 |

Subtraction | – | Subtract second operand from first operand. | B – A = 30 |

Multiplication | * | Multiply both operands. | A * B = 180 |

Division | / | Divide numerator by de-numerator. | B / A = 2 |

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

#### Create a simple C program to understand Arithmetic operators.

```
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter the value of A:");
scanf("%d",&a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("A = %d\n", a);
printf("B = %d\n\n", b);
//preform Addition operation
c = a + b;
printf("A + B = %d\n", c);
//perform Subtract operation
c = b - a;
printf("B - A = %d\n", c);
//perform Multiply operation
c = a * b;
printf("A * B = %d\n", c);
//perform division operation
c = b / a;
printf("B / A = %d\n", c);
//perform modulus operation
c = b % a;
printf("B %% A = %d\n", c);
return 0;
}
```

##### Output:-

Output

Enter the value of A:30

Enter the value of B:60

A = 30

B = 60

A + B = 90

B – A = 30

A * B = 1800

B / A = 2

B % A = 0

### Relational Operators :-

Relational operators will used to perform comparison on two operands. They are important for making decisions. They allow us compare numeric and char values to determine if one is greater than, less than, equal to, or not equal to another. It is always produce ‘Boolean Result(T/F)’.

The following table show all the Relational operators with example.

Assume variable **A** holds 40 and variable **B** holds 50 then:−

Operator Type | Operator | Operation | Example |

Greater than | > | 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 True. |

Less than | < | 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 not True. |

Greater than or equal to | >= | 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 True. |

Less than or equal to | <= | 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 not True. |

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

Not equal to | != | 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. |

#### Create simple C programs to understand Relational operators.

##### 1.Grater than(>) Operator:-

```
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("A = %d\n", a);
printf("B = %d\n", b);
if(a>b)
{
printf("A is grater than B \n");
}
else
{
printf("A is not grater than B\n");
}
return 0;
}
```

##### Output:-

Output

Enter the value of A:20

Enter the value of :10

A = 20

B = 10

A is grater then B

Enter the value of A:10

Enter the value of B:20

A = 10

B = 20

A is not grater then B

##### 2. Less than(<) Operator:-

```
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("A = %d\n", a);
printf("B = %d\n", b);
if(a<b)
{
printf("A is less than B \n");
}
else
{
printf("A is not less than B\n");
}
return 0;
}
```

##### Output:-

Output

Enter the value of A:10

Enter the value of B:15

A = 10

B = 15

A is less than B

Enter the value of A:15

Enter the value of B:10

A = 15

B = 10

A is not less then B

##### 3. Grater than or equal to(>=) Operator:-

```
#include<stdio.h>
int main()
{
int a, b,c;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("A = %d\n", a);
printf("B = %d\n", b);
if(a >= b)
{
printf("A is either grater than or equal to B\n");
}
else
{
printf("A is neither grater than nor equal to B\n");
}
return 0;
}
```

##### Output:-

Output

Enter the value of A:15

Enter the value of B:10

A = 15

B = 10

A is either grater than or equal to B

Enter the value of A:10

Enter the value of B:15

A = 10

B = 15

A is neither grater than nor equal to B

Enter the value of A:10

Enter the value of B:10

A = 10

B = 10

A is either grater than or equal to B

##### 4. Less than or equal to(<=) Operator:-

```
#include<stdio.h>
int main()
{
int a, b,c;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("A = %d\n", a);
printf("B = %d\n", b);
if(a <= b)
{
printf("A is either less than or equal to B\n");
}
else
{
printf("A is neither less than nor equal to B\n");
}
return 0;
}
```

##### Output:-

Output

Enter the value of A:10

Enter the value of B:15

A = 10

B = 15

A is either less than or equal to B

Enter the value of A:12

Enter the value of B:12

A = 12

B = 12

A is either less than or equal to B

Enter the value of A:15

Enter the value of B:10

A = 15

B = 10

A is neither less than nor equal to B

##### 5. Equal to(==) Operator:-

```
#include<stdio.h>
int main()
{
int a, b,c;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("A = %d\n", a);
printf("B = %d\n", b);
if(a==b)
{
printf("A is equal to B\n");
}
else
{
printf("A is not equal to B\n");
}
return 0;
}
```

##### Output:-

Output

Enter the value of A:10

Enter the value of B:10

A = 10

B = 10

A is equal to B

Enter the value of A:10

Enter the value of B:15

A = 10

B = 15

A is not equal to B

##### 6. Not equal to(!=) Operator:-

```
#include<stdio.h>
int main()
{
int a, b,c;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("A = %d\n", a);
printf("B = %d\n", b);
if(a!=b)
{
printf("A is not equal to B\n");
}
else
{
printf("A is equal to B\n");
}
return 0;
}
```

##### Output:-

Output

Enter the value of A:10

Enter the value of B:12

A = 10

B = 12

A is not equal to B

Enter the value of A:10

Enter the value of B:10

A = 10

B = 10

A is equal to B

```
Enter the value of A:10
Enter the value of B:12
A = 10
B = 12
A is not equal to B
Enter the value of A:10
Enter the value of B:10
A = 10
B = 10
A is equal to B
```

### Logical Operator :-

Logical operators will used to combine result of two relational expressions.logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.

The following table show all the Logical operators with example.

Operator Type | Operator | Operation | Example |

Logical AND | && | True only if all operands are true | If a = 3, b = 2 and c = 1 then, expression `(a>b) && (c<a) is` equals to 1. |

Logical OR | || | True only if either one operand is true | If a = 1, b = 2 and c = 3 then, expression (a==b) || (c>a) is equals to 0. |

Logical NOT | ! | True only if the operand is 0 | If c = 5 then, expression `!(c==5)` equals to 0. |

#### Create simple C programs to understand Logical operators.

##### Logical AND(&&) Operator:-

```
#include<stdio.h>
int main()
{
int a, b, c, d;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("Enter the value of C:");
scanf("%d", &c);
d = (a > b) && (c < a);
printf("(a > b) && (c < a) is %d\n", d);
return 0;
}
```

##### Output:-

Output

Enter the value of A:3

Enter the value of B:2

Enter the value of C:1

(a > b) && (c < a) is 1

Enter the value of A:3

Enter the value of B:2

Enter the value of C:4

(a > b) && (c < a) is 0

##### Logical OR(||) Operator:-

```
#include<stdio.h>
int main()
{
int a, b, c, d;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("Enter the value of C:");
scanf("%d", &c);
d = (a == b) || (a > c);
printf("(a == b) || (c > a) is %d\n", d);
return 0;
}
```

##### Output:-

Output

Enter the value of A:10

Enter the value of B:20

Enter the value of C:5

(a == b) || (c > a) is 1

Enter the value of A:10

Enter the value of B:20

Enter the value of C:30

(a == b) || (c > a) is 0

```
Enter the value of A:10
Enter the value of B:20
Enter the value of C:5
(a == b) || (c > a) is 1
Enter the value of A:10
Enter the value of B:20
Enter the value of C:30
(a == b) || (c > a) is 0
```

##### Logical NOT(!) Operator-

```
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
c = !(a == b);
printf("!(a == b) is %d\n", c);
return 0;
}
```

##### Output:-

Output

Enter the value of A:10

Enter the value of B:15

!(a == b) is 1

Enter the value of A:10

Enter the value of B:10

!(a == b) is 0

### Bitwise Operators :-

Bitwise operators are special operator set provided by ‘C.’ They are used in bit level programming. These operators are used to manipulate bits of an integer expression.

The following table show all the Bitwise operators with example.

Operator Type | Operator | Meaning | Example |

Bitwise AND | & | Bitwise AND operator | A = 20 B = 21 And = 20 |

Bitwise exclusive OR | ^ | Bitwise exclusive OR operator | A = 20 B = 21 Exclusive-OR = 1 |

Bitwise OR | | | Bitwise OR operator | A = 20 B = 21 OR = 21 |

Shift right | >> | Right shift operator | A = 20 Right shift = 5 |

Shift left | << | Left shift operator | A = 20 Left shift = 80 |

Bitwise complement | ~ | Binary One’s Complement Operator is a unary operator | A = 10 complement = -11 |

#### Create a simple C program to understand Bitwise operators.

```
#include<stdio.h>
int main()
{
int a=20; // 20 = 010100
int b=21; // 21 = 010101
int c;
printf("A = %d\n", a);
printf("B = %d\n", b);
c = a & b; // 20 = 010100
printf("And = %d\n", c);
c = a ^ b; // 1 = 0001
printf("Exclusive-OR = %d\n",c);
c = a | b; // 21 = 010101
printf("OR = %d\n", c);
a=20; // 20 = 010100
printf("A = %d\n", a);
c = a >> 2; // 5 = 000101
printf("Right shift = %d\n", c);
c = a << 2; // 80 = 101000
printf("Left shift = %d\n", c);
a=10; // 10 = 1010
printf("A = %d\n", a);
c = ~(a);
printf("complement = %d\n", c);
return 0;
}
```

##### Output:-

Output

A = 20

B = 21

And = 20

Exclusive-OR = 1

OR = 21

A = 20

Right shift = 5

Left shift = 80

A = 10

complement = -11

### Assignment Operators:-

An assignment operator is used for assigning a value to a variable of expression.

The following table show all the Assignment operators with example.

Operator Type | Operator | Operation | 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 += B is equivalent to C = C + B |

Subtract AND assignment operator | -= | It subtracts the right operand from the left operand and assigns the result to the left operand. | B -= A is equivalent to B = B – 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 /= B is equivalent to C = C / B |

Modulus AND assignment operator | %= | It takes modulus using two operands and assigns the | C %= A is equivalent to C = C % A |

#### Create a simple C program to understand Assignment operators.

```
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Value of A = %d\n", a);
//simple assignment operator
c = a;
printf("c = %d\n", c);
//Add AND assignment operator
c += a;
printf("c = %d\n", c);
//Subtract AND assignment operator
c -= a;
printf("c = %d\n", c);
//Multiply AND assignment operator
c *= a;
printf("c = %d\n", c);
//Divide AND assignment operator
c /= a;
printf("c = %d\n", c);
//Modulus AND assignment operator
c %= a;
printf("c = %d\n", c);
return 0;
}
```

##### Output:-

Output

Enter the value of A:5

Value of A = 5

c = 5

c = 10

c = 5

c = 25

c = 5

c = 0

### Increment/Decrement Operators:-

Increment operator is used to increase the value of variable by one and represented by ++. Decrement operator is used to decrease the value of variable by one and represented by –.

The following table show the Increment/Decrement operators with example.

Operator Type | Operator | Operation | Example |

Increment operator | ++ | increase the value of variable by one. | a = 4 b = ++a b = 5 |

Decrement operator | — | decrease the value of variable by one. | a = 6 b = –a b = 5 |

#### Create a simple C program to understand Assignment operators.

```
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Value of A = %d\n", a);
//increment operator
b = ++a;
printf("B = %d\n", b);
//decrement operator
b = --a;
printf("B = %d\n", b);
return 0;
}
```

##### Output:-

Output

Enter the value of A:5

Value of A = 5

B = 6

B = 5

### Conditional Operators:-

The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.

#### Create a simple C program to understand Conditional operators.

```
#include<stdio.h>
int main()
{
int a,b;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("Value of A = %d\n", a);
printf("Value of B = %d\n", b);
(a > b) ? printf("A is greater than B that is %d > %d", a, b)
: printf("B is greater than A that is %d > %d",b, a);
return 0;
}
```

output:-

Output

Enter the value of A:15

Enter the value of B:10

Value of A = 15

Value of B = 10

A is greater than B that is 15 > 10

Enter the value of A:10

Enter the value of B:15

Value of A = 10

Value of B = 15

B is greater than A that is 15 > 10

### Special Operators:-

The following table show all the Special operators with example.

Operator | Operation | Example |

sizeof | Returns the size of an variable | sizeof(A) return size of the variable A |

& | Returns the address of an variable | &A ; return address of the variable A |

* | Pointer to a variable | *A ; will be pointer to a variable A |

#### Create a simple C program to understand & and * operator.

```
#include<stdio.h>
int main()
{
int *ptr, a;
printf("Enter the value of A:");
scanf("%d", &a);
printf("Value of A = %d\n", a);
//address of a is assigned to ptr
ptr = &a;
//display the value of a using ptr variable
printf("%d", *ptr);
return 0;
}
```

##### Output:-

Output

Enter the value of A:10

Value of A = 10

10

Create a simple C program to understand sizeof operator.

```
#include<stdio.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for integer data type = %d\n", sizeof(a));
printf("Storage size for char data type = %d\n", sizeof(b));
printf("Storage size for float data type = %d\n", sizeof(c));
printf("Storage size for double data type = %d\n", sizeof(d));
return 0;
}
```

##### Output:-

Output

Storage size for integer data type = 4

Storage size for char data type = 1

Storage size for float data type = 4

Storage size for double data type = 8

Amazing Sir, Thanks