# C Operators

Like other programming languages, C language also supports the set of operators to perform a different kind of operations on variables.

Now, the question arises, What is Operator?

### Operator

An **Operator** is defined as a symbol that represents some mathematical or logical operations. It can take one or more operands to perform its operations.

For example, Arithmetic Operators perform Arithmetic operations such as Addition(+) Operator performs the addition operation.

### Operand

**Operands** are the variables or objects which are used by Operators to evaluate the expression or operation.

For example: ‘y + x’ is an expression, where ‘x’ and ‘y’ are operands and ‘+’ is an Operator. All expressions must have at least one operand.

### Types of Operators

The operators can be classified like this:

- Arithmetic Operators
- Relational or Comparison Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc/Special Operators

Now, we are gonna learn about all these operators one by one.

### Arithmetic Operators

An **Arithmetic Operator** performs some mathematical operations such as addition, subtraction, multiplication, and division.

Here is the table of following Arithmetic Operators which are supported by C language:

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

+ | Adds two operands. | A + B |

– | Subtracts second operand from the first. | A − B |

* | Multiplies both operands. | A * B |

/ | Divides numerator by denominator and gives us the Quotient. | B / A |

% | Modulus Operator: calculate remainder of after an integer division. | B % A |

++ | Increment operator increases the integer value by one. | A++ |

— | Decrement operator decreases the integer value by one. | A– |

###### For example:

1 2 3 4 5 6 7 8 9 |
#include<stdio.h> int main() { int x=1; int y=5; int result=x+y; printf("sum of 5 and 1 is %d",result); return 0; } |

### Relational Operators :

**Relational Operators** check whether the relation between two or more operands is True or False.

Here, you can see all the relational operators supported by C with the help of the given table.

Assuming that the variable A holds 10 and variable B holds 20 then −

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

== | If the values of both operands are equal, then the condition becomes true, and return 1 otherwise return 0(false). | A == B. |

!= | If values are not equal, then the condition becomes true and return 1 otherwise return 0(false). |
A != B. |

> | If the value of left operand is greater than the value of right operand, then the condition becomes true. | A > B |

< | If value of left operand is less than the value of right operand, then the condition becomes true and return 1 otherwise return 0(false). | A < B |

>= | Either the value of left operand is greater than or equal to the value of right operand, then the condition becomes true and return 1 otherwise return 0(false). | A>=B |

<= | Either the value of left operand is less than or equal to the value of right operand, then the condition becomes true and return 1 otherwise return 0(false). | A<=B |

###### For example:

1 2 3 4 5 6 7 8 9 |
#include<stdio.h> int main() { int x=1; int y=5; int result=x<y;//This checks wether the condition is true or not printf("Result %d",result);//If the condition is true, this will print 1 otherwise 0 means false. return 0; } |

### Logical Operators

Logical Operators are mainly used to control program flow. The concepts of logical operators are simple.**Logical Operators** allow a program to make a decision based on multiple conditions. Each operand is considered a condition that can be evaluated to a true or false value. Then the value of the conditions is used to determine the overall value.

Here is the table of all Logical Operators supported by C language:

We considered that variable A holds 1 and variable B holds 0, then –

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

&& | Logical AND operator. If both the operands are non-zero, then the condition becomes true and return 1 otherwise false 0. | A && B |

|| | Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true and return 1 otherwise false 0. | A || B |

! | Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. |
!A && B |

###### For example:

1 2 3 4 5 6 7 8 9 |
#include<stdio.h> int main() { int x=1; int y=5; int result=(x<y)||(x>y);//Logical OR Operator printf("result= %d",result); return 0; } |

### Bitwise Operators

A Bitwise Operator used to perform bitwise operations on bit patterns. Unlike common logical Operators, which work with bytes, Bitwise Operators can check wor set each of the individual bits within a byte.

If the corresponding bits of two operands is 1, then the output of bitwise AND is 1. If either bit of an operand is 0, the result of the corresponding bit is evaluated to 0.

Decimal values are converted into binary values which are the sequence of bits and bitwise Operators work on these bits.

### Bitwise operators in C language are

- & (bitwise AND)
- | (bitwise OR)
- ~ (bitwise OR)
- ^ (XOR)
- << (left shift)
- >> (right shift).

**TRUTH TABLE FOR BITWISE OPERATION & BITWISE OPERATORS:**

x | y | x | y | x & y | x ^ y |
---|---|---|---|---|

0 | 0 | 0 | 0 | 0 |

0 | 1 | 1 | 0 | 1 |

1 | 0 | 1 | 0 | 1 |

1 | 1 | 1 | 1 | 0 |

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

& | Binary AND Operator copies a bit to the result if it exists in both operands. |
A & B |

I | Binary OR Operator copies a bit if it exists in either operand. |
A I B |

^ | Binary XOR Operator copies the bit if it is set in one operand but not both. |
A ^ B |

~ | Binary Ones Complement Operator is unary and has the effect of ‘flipping’ bits. |
~ A |

<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. |
A << 1 |

>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. |
A >> 1 |

###### For example:

1 2 3 4 5 6 7 8 9 10 |
#include<stdio.h> int main() { //consider x and y equal to 40 and 80 respectively, then the binary valur of x and y will be int x=00101000; int y=01010000; int result=x&y;//Bitwise AND Operator printf("Result= %d",result);//Answer will be 00000000(in binary) and 0(in decimal). return 0; } |

### Assignment Operators

With the help of **Assignment Operators**,we can assign a new value to a variable.

The following table lists the assignment operators supported by the C language:

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

= | Simple assignment operator. Assigns values from right side operands to left side operand |
C = A + B |

+= | Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. |
C += A |

-= | Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. |
C -= A |

*= | Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. |
C *= A |

/= | Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. |
C /= A |

%= | Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. |
C %= A |

<<= | Left shift AND assignment operator. | C << |

>>= | Right shift AND assignment operator. | C >>=5 same as C = C >>5 |

&= | Bitwise AND assignment operator. | C &=5 same as C = C & 5 |

^= | Bitwise exclusive OR and assignment operator. | C ^= 2 same as C = C ^ 5 |

|= | Bitwise inclusive OR and assignment operator. | C |= 2 same as C = C | 5 |

###### For example:

1 2 3 4 5 6 7 8 9 |
#include<stdio.h> int main() { int x; x=1; x+=5;//This is an Addition Assignment Operator printf("x= %d",x); return 0; } |

### Misc/Special Operators

We have been gone through to most of the operators supported by C language. Besides of these Operators discussed above, here are some other important operators including sizeof and ? : supported by the C Language.

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

sizeof() | Returns the size of a variable. Size is the bytes reserved by that variable’s datatype. | sizeof(a) |

& | Returns the address of a variable. | &a; |

* | Pointer to a variable. | *a; |

? : | Conditional Expression. | (a > b) ? X : y, If Condition is true ? then value X : otherwise value Y |

###### For example:

1 2 3 4 5 6 7 8 9 |
#include<stdio.h> int main() { int x=1; int y; y=sizeof x; printf("y= %d",y); return 0; } |

**Congratulations! Chapter Finished. Do you want to practice more?**