# Operators: C programming language

|**Operators** are specific symbols that give instructions to the compiler to operate on the data or operands. **Operands** are the entities on which operation is being performed. Operators operate on the operands to produce a result. For example: **c=a+b**; where a=5, b=10. So, here ‘+’ is an operator which operates on the operands, i.e., ‘a’ and ‘b’ and results in the output c(=15). C Programming Language offers us the following operators to work with:

**Arithmetic Operators****Logical Operators****Increment Decrement Operators****Conditional Operators or ternary operators****Relational Operators****Assignment Operators****Bitwise Operators****Other Operators**

Let’s go through all these operators and their use in detail:

### Arithmetic Operators

**Arithmetic operators** are used to perform arithmetic operations in C like addition, subtraction, multiplication, etc. It takes two or more numerical values(also in the form of variables) and a single numerical result. Here are the arithmetic operators that are offered by the C Programming Language:

**+**: Used for the addition of operands or string concatenation. It gives us the sum of the operands.**–**: Used for Subtraction of operands. It gives us the difference of the operands.*****: Used for Multiplication of operands. It gives us the product of the operands**/**: Used for Dividing the operands. Gives us the quotient.**%**: Used for getting the remainder after dividing the operands.

The following program illustrates the use of arithmetic operators:

#include <stdio.h> #include <stdlib.h> int main() { int a = 16, b = 5, c = 0; c=a+b; printf("the sum is: %d\n", c); c=a-b; printf("the difference is: %d\n", c); c=a*b; printf("the product is: %d\n", c); c=a/b; printf("the quotient is: %d\n", c); c=a%b; printf("the remainder is: %d\n", c); return 0; }

**Output:-**
the sum is: 21
the difference is: 11
the product is: 80
the quotient is: 3
the remainder is: 1

Here, the arithmetic operators operate on the given integer values and produce the output accordingly.

### Logical Operators

**Logical Operators** results in either **0** or **1**. **0** stands for *false* and 1 stands for *true*. Here are the logical operators offered by the C Programming Language:

**!**: The**Logical NOT**operator. Also known as the*negation*operator. It negates the value of the operand or reverses its logical state i.e. True to False and False to True.**||**: The**Logical OR**operator. It results in*true*(or 1) if both the operands or any one of the operand is*true*(value is non-zero) and results in*false*(or 0) if both the operands are*false*.**&&**: The**Logical AND**operator. It results in*true*(or 1) if both the operands are*true*(value is non-zero) otherwise*false*(or 0).

The following program illustrates the use of **logical operators**:

#include <stdio.h> #include <stdlib.h> int main() { int a=16,b=5,c=0,d; d= a&&c; printf("a&&c results in: %d\n",d); d=a&&b; printf("a&&b results in: %d\n",d); d=a||c; printf("a||c results in: %d\n",d); d=!b; printf("!b results in: %d\n",d); return 0; }

**Output:-**
a&&c results in: 0
a&&b results in: 1
a||c results in: 1
!b results in: 0

Here the logical operators operate on the given values(operands) to produce the given output accordingly.

### Increment and Decrement Operators

The **increment operator** **‘++’** is used to increment the value of the operand by** 1** at a time. Similarly, the **decrement operator ‘–‘** is used to decrement the value of the operand by **1** at a time. They can be used in two ways:

: They can be used as a prefix to the operands, for example-**prefix****++a**,**–x**; This means*‘increase/decrease then use’*, i.e., first increase/decrease the value of the operand by 1 accordingly and then use it’s incremented/decremented value.: They can be used as a postfix to the operands. for example-**postfix****a++**,**x–**; This means*‘use and increase/decrease’*, i.e., first use the value of the operand and then increment/decrement it by 1 accordingly.

The following program illustrates the use of **increment/decrement operators**:

#include <stdio.h> int main() { int a=5,b=16; printf("postfix increment: %d\n",a++); printf("prefix increment: %d\n",++a); printf("prefix decrement: %d\n",--b); printf("postfix decrement: %d\n",b--); return 0; }

**Output:-**
postfix increment: 5
prefix increment: 7
prefix decrement: 15
postfix decrement: 15

In the first “printf()” we see that **a++** results in **5**. This is because the value of a is first being printed and then it is incremented which makes **a=6**. In the next line **++a** results in **7** because the value of a is incremented first which makes **a=7** and then that value is printed. Similarly, in the next line we see that** –b** results in** 15** because the value of b is decremented first(now **b=15**) and then it is printed. And finally in the last line the value of b is printed first(i.e, **b=15**) and then decremented to **b=14**.

### Conditional Operator or Ternary Operator

A **Conditional Operator** works on three operands. It uses the symbols ?: . Here is the syntax of using a ternary operator:

**condition ? expression1 : expression2;**

Here, the* condition *is the conditional statement which results in *true *or* false.* Accordingly, *expression1* is the result if the *condition* is *true* and *expression2* is the result if the *condition* is *false*.

We can also have nested ternary operator. The syntax is as follows:

**condition1 ? (condition2 ? expression1 : expression2) : expression3;**

The following program illustrates the use of **conditional operators**:

#include <stdio.h> int main() { int a = 17, b = 16,c = 30, d; d = (a>b?a:b);//simple ternary operator printf("the bigger number is: %d\n",d); d = (a>b?(a>c?a:c):(b>c?b:c));//nested ternary operator printf("the largest number is: %d",d); return 0; }

Output:-the bigger number is: 17 the largest number is: 30

Here the conditional operators operate on the given values(operands) to produce the given output accordingly.

### Relational Operators

A** Relational Operator** checks the relationship between two operands and returns 1(for *true*) or 0(for *false*). Here are the Relational Operators offered by the C Programming Language:

**==**: Checks if the two operands are*equal*or not. Returns*true*if the operands are equal and*false*if they are not equal.**<=**: Returns*true*if the left operand is*less than or equal*to the right operand.**>=**: Returns*true*if the left operand is*greater than or equal*to the right operand.**!=**: Returns*true*if the two operands are*not equal*.**<**: Returns*true*if the left operand is*less than*the right operand.**>**: Returns*true*if the left operand is*greater than*the right operand.

### Assignment Operator

An **Assignment Operator** assigns a value to the variable. Here are the Assignment Operators offered by the C Programming Language:

**=**: Assigns the value of the right operand to the left operand. Eg:**a=b; a=10;****+=**: Adds up the left operand and the right operand and assigns that value to the left operand. Eg:**a+=b;**which is**a=a+b;****-=**: Subtracts the right operand from the left operand and assigns that value to the left operand. Eg:**a-=b;**which is**a=a-b;*****=**: Multiplies the left and the right operand and stores the value in the left operand. Eg:**a*=b;**which is**a=a*b;****/=**: Divides the left operand by the right operand and stores the value in the left operand. Eg:**a/=b;**which is**a=a/b;**

### Bitwise Operator

A **Bitwise Operator** performs mathematical operations at bit level. Or in simple words, it performs the operation bit by bit which makes the computation a faster process. Here are the **Bitwise Operators** offered by the C Programming Language:

**&**: It is the**Bitwise AND**Operator. ANDs the two operands and returns the value.**|**: It is the**Bitwise OR**Operator. ORs the two operands and returns the value.**~**: It is the**Bitwise Complement**operator. Complements the operand and returns the value.**^**: It is the**Bitwise Exclusive OR**(XOR) Operator. Returns the XOR of the two operands.**>>**:**Bitwise Right shift**. Right shifts the operand by the given value. Eg:**a>>2;**which means it right shifts the value of the variable by 2.**<<**:**Bitwise Left Shift**. Left shifts the operand by the given value. Eg:**a<<3;**which means it left shifts the value of the variable by 3.

The following program illustrates the use of **Bitwise**** operators**:

#include <stdio.h> #include <stdlib.h> int main() { int a=5, b=16, c; c = a & b; printf("The Bitwise AND: %d\n", c); c = a|b; printf("The Bitwise OR: %d\n", c); c = a ^ b; printf("The Bitwise Exclusive OR: %d\n", c); c = ~b; printf("The Bitwise Complement: %d\n", c); c = b >> 2; printf("The Bitwise right shift: %d\n", c); c = a << 3; printf("The Bitwise left shift: %d\n", c); return 0; }

**Output:-**
The Bitwise AND: 0
The Bitwise OR: 21
The Bitwise Exclusive OR: 21
The Bitwise Complement: -17
The Bitwise right shift: 4
The Bitwise left shift: 40

Here the bitwise operators operate on the given values(operands) to produce the given output accordingly.

### Miscellaneous Operators

Some of the other operators that C Programming Language offers are:

: This operator returns the size of the operand. Eg:**sizeof****sizeof(a);**returns the size in bytes of the variable.**&**: This operator gives the address of the operand. Eg:**&a;**returns the address in memory of the variable.

**So that’s all for this tutorial. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. **

**Do not forget to SHARE and SUBSCRIBE.**

**Keep Coding!! Happy Coding!!**