**Introduction**

Before learning operators in c programming, we should know what is the meaning of operators. The operator is nothing but a symbol which can be used in mathematical or logical expressions. We know, an expression is the combination of operand and operators. So, operators work on operators.

**Also read:** C Program to Find the Sum of Cubes of Elements in an Array

For example,** c=a+b** is an expression. In this expression, a, b and c are **operands** and +, = signs are **operators.** In every programming language support operators because we computer can perform arithmetic and operations.

**Operators in C Programming**

In c programming, there are total eight type of operators and they are:

- Arithmetic Operators.
- Assignment Operators.
- Relational Operators.
- Logical Operators.
- Increment and Decrement Operator.
- Conditional Operator.
- Bitwise Logical and Shift Operators.
- Special Operators.

**Arithmetic Operators in C**

Arithmetic operators are used to arithmetic operations such as addition, subtraction, multiplication and division. In c language, there is one more arithmetic operator and that is modulo division operator. See the following table.

Operator | Meaning |

+ | Addition |

– | Subtraction |

* | Multiplication |

/ | Division |

% | Modulo Division |

In c programming, we have a new operator which we do not use in algebraic expression. This new operator is known as **Modulus operator or Modulo division operator.** When we need remainder after the division, then we can use this operator.

**Also Read:** Interview Questions On C

For example, suppose, we have a=5 and b=2 and both the variables are integers, then see the following table.

Operation | Result |

a+5 | 7 |

a-b | 3 |

a*b | 10 |

a/b | 2 |

a%b | 1 |

In the above table, a%b means 5%2. Here, we are getting remainder 1 after performing division operation. There are various occasions where we have to use modulo division operator. For example, suppose, we need to find even or odd number or reversing digits of a number. Let’s see the c program for arithmetic operators.

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

**Output:**

```
5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2
5 % 2 = 1
```

Also read: C Program to Print Numbers Except Multiples of n

**2. Assignment Operators in **C

The assignment operator(=) is used to assign the value of an expression or the variable or the constant. In other words, we can store the value of an expression or the variable or the constant to the another variable. See the following syntax.

`variable = expression or variable or constant`

For example,

```
a = 5;
sum = a + b + c;
add = sum;
```

In the first statement, we are assigning the value 5 to the variable 5. That means we are storing the value of 5 in the variable a. The Second statement is assigning the value of expression i.e. a+b+c to the variable sum and the statement assigning the value of the variable sum to the variable add.

These are all statements of simple assignment statements.

**Short-hand Assignment Operator in C**

This is not a different operator. It is the shortest version of simple assignment operator. What is short-hand assignment operator?

The **op=** is short-hand assignment operator where op is nothing but an arithmetic operator. For example, **a=a+b** can be written as **a+=b**. Here, op is + operator. There is one condition to use short-hand assignment operator and that is a simple assignment statement must contain a common operand to both sides. Here, a is the common operand that you can observe in both the side. Now, let’s a few more examples.

Simple Assignment | Short-Hand Assignment |

a=a+5 | a+=5 |

sum=sum/5; | sum/=5; |

a=a+(b*5); | a+=(b*5) |

I hope, you have understood assignment operators in c programming. Now, we are moving to our next operator.

**3. Relational Operators in C Programming**

These operators are used when we need an answer in true or false. To check the relation between two entities, relational operators can be used. Following is the list of relational operators in c programming.

Operator | Meaning |

< | is less than |

<= | is less than or equal to |

> | is greater than |

>= | is greater than or equal |

== | is equal to |

!= | is not equal to |

The statement (a>b) will evaluate to true when the value of a is greater than b otherwise, evaluates to false. We can use these operators while taking decisions using decision statements and loop statements. See the following the c program.

**C program to find greater number between two numbers**

```
#include<stdio.h>
int main()
{
int a,b;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
if(a>b)
printf("%d is greater than %d",a,b);
else if(a<b)
printf("%d is greater than %d",b,a);
else
printf("Both numbers are equal");
return 0;
}
```

**Output**

```
Enter any two numbers
6
7
7 is greater than 6
```

I hope, you are getting whatever I am trying to explain. Now, let’s move to the next operator in c programming.

Also read: Switch Case in C Program to Calculate Area of Circle and Triangle

**4. Logical Operators in C Programming**

Most of the time, we write a c program where we use more than one conditions. For example, suppose, I need to write a program to find the greatest number between three numbers. Just consider, we have three numbers and they are a, b and c.

So, here, we will compare a with b and then c. If a is greater than both, a and b, then we can say a is the greatest number. Similarly, we will do the same thing with b and c. Therefore, in such type of situations, we can use logical operators in c programming. If you need to understand this well, just see the following table.

Operator | Meaning |

&& | Logical AND |

|| | Logical OR |

! | Logical NOT |

In the above table, we can see that there are three logical operators and they are logical AND, logical OR and logical NOT. Let’s see the meaning of each logical operator.

**Logical AND**

When all the inputs are TRUE, then and only then output is TRUE, otherwise the output is FALSE. In other words, we can say, if anyone input is FALSE then output is FALSE, otherwise the output is TRUE.

**Logical OR**

When all the inputs are FALSE, then and only then output is FALSE, otherwise the output is TRUE. In other words, we can say, if anyone input is TRUE then output is TRUE, otherwise the output is FALSE.

**Logical NOT**

This is a unary operator. That means it works on an only single operand. Here, single operand means any expression which has true or false value. This operator can change true value to false and false value to true.

For example, see the following program which find greatest number amongst three numbers.

```
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter any three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if((a>b) && (a>c))
printf("%d is greatest",a);
else if((b>a) && (b>c))
printf("%d is greatest",b);
else
printf("%d is greatest",c);
return 0;
}
```

**Output**

```
Enter any three numbers
5
10
7
10 is greatest
```

**5. Increment and Decrement Operators in C Programming**

++ and — are known as increment and decrement operators in c programming respectively. So, here ++ means add 1 and — means subtract 1. Both of these operators are unary. It takes only one operand.

For example, ++a or a++ means a=a+1. Similarly, –a or a– means a=a-1. See the following c program for illustrating this concept.

```
#include<stdio.h>
int main()
{
int a=5,b=10;
printf("a++ = %d and --b = %d",a++,--b);
return 0;
}
```

In the above program, we have declared two variables, a and b. The initial value of a is 5 and b is 10. In the printf() statement, we are incrementing the value of a and decrementing the value of b and then displaying the value of a and b. See the following output.

`a++ = 6 and --b = 9`

But, increment and decrement operators are not as simple as that. We have to study this in more detail. The position of these operators decides its working. When we use these operators in an expression, then it has two types and they are pre-increment and post-increment for increment operator and in case of decrement operator, it has pre-decrement and post-decrement.

**What is pre-increment and post-increment operators in c programming?**

When increment operator i.e. ++ comes before an operand, then it becomes pre-increment operator and if it comes after an operand, then we can say, it is an post-decrement operator. For example, ++a is a pre-increment operator and a++ is a post-increment operator. When we use this operator individually, then there is no issue. It work same. But, its working change when we use this operator in an expression.

In pre-increment operator, **the value of an operand increment first and then that updated value will be used in an expression.**

In post-increment operator, **the value of an operand increment later, but before that the original value will be used in an expression.**

Let’s understand this using the program.

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

In the above c program, you can observe that I have used pre-increment operator and post-increment operator both. The statement, b=++a means the value of a is incremented first and then that updated value of a is assigned to b. That means, the output will be a=6 and b=6.

But, in the next expression i.e. b=a++, the value of a is assigned first and after that the value of a will be incremented. That means, the output will be a=7 and b=6;

**Output**

```
a = 6 and b = 6
a = 7 and b = 6
```

**What is pre-decrement and post-decrement operators in c programming?**

This is same as above. Only the difference is, instead of ++, we have to use –. All the working will be same.

In pre-decrement operator, **the value of an operand decrement first and then that updated value will be used in an expression.**

In post-decrement operator, **the value of an operand decrement later, but before that the original value will be used in an expression.**

Let’s see another program.

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

**Output**

```
a = 4 and b = 4
a = 3 and b = 4
```

Try this program and I hope, you will understand this concept of logical operators in c programming. Let’s move to our next operator.

Also read: LCM of Two Numbers in C Programming

**6. Conditional Operator in C Programming**

The conditional operator (?:) is also known as a ternary operator in c programming. It works on three operands. See the following syntax.

`exp1 ? exp2 : exp3;`

Here, exp1, exp2 and exp3 are three different expressions. The exp1 is evaluates to true or false value. When exp1 is true, then exp2 will be executed, otherwise exp3 will be executed.

**C program to find greater number between two numbers using Conditional Operator**

```
#include<stdio.h>
int main()
{
int a,b,max;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
max=(a>b)?a:b;
printf("%d is greatest number between %d and %d",max,a,b);
return 0;
}
```

In the above, we have used a conditional operator to find a** **greater number between two numbers. The statement **max=(a>b)?a:b;** is finding greatest number. How? Just compare this statement with the above syntax. You can find that the variable (a>b) is exp1 which evaluates to a true or false. **When the value of (a>b) is true, then the value of a is assigned to the variable max, otherwise, the value of b is assigned to the variable max.**

**Output**

```
Enter any two numbers
6
7
7 is greatest between 6 and 7
```

**7. Bitwise Logical and Shift Operators in C Programming**

These operators works on binary digits i.e. bits of an operand. The bitwise operators works exactly like logical operators in c programming, the only difference is logical operator works on operands. The shift operators are used to shift the bits towards left or right. Now, see the following table.

Operator | Meaning |

& | Bitwise Logical AND |

| | Bitwise Logical OR |

~ | Bitwise Logical NOT |

^ | Bitwise Logical XOR |

<< | Bitwise Shift Left |

>> | Bitwise Shift Right |

**Bitwise Logical AND (&)**

When all the inputs are 1 then and only then the output is 1, otherwise output is 0.

**Bitwise Logical OR (|)**

When all the inputs are 0 then and only then the output is 0, otherwise output is 1.

**Bitwise Logical NOT (&)**

This is also called as bitwise compliment operator. When input bit is 0, then output is 1 and for input 1, it makes output 0.

**Bitwise Logical XOR (^)**

When all the inputs are same then the output is 0, otherwise output is 1.

**Bitwise Shift Left (<<)**

This operator is used to shift the bits to the left by n bits.

**Bitwise Shift Right (>>)**

#### This operator is used to shift the bits to the right by n bits.

See the following program.

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

**Output**

```
(a & b) = 10
(a | b) = 15
(a ^ b) = 5
~(a & b) = -11
(a << 3) = 120
(b >> 3) = 1
```

**8. Special Operators in C Programming**

These operators are special because it has some special meaning. We can not use these operators in an expression. The &, * and sizeof() are special operators.

**Address of operator (&)**

This operator is also known as the reference operator. This operator deals with the address of the variable.

**Dereference operator (*)**

This operator is also known as the value at the address operator. This operator deals with the value stored at the particular address of the variable.

**The sizeof( ) operator**

This operator is used to read the size of the variable.

These operators can be used as per our requirement. I hope you have understood operators in c programming. For any query, please feel free to contact me. I have made two videos on this topic. If you know Hindi language, then please go through this.

Thank you.