Follow Us:

# How do the operators work in c? | Operators in C

Well, it is just like the case of the manufacturing industry, some ingredients are added to a machine and the further output to be produced is taken care of by the machine. So, operators are basically just like machines numbers are added to it and operators know what output to produce. Hello guys, today we’ll be dealing with the concept of operators in the C language. Well, before starting I would like to state that this concept is not a hard nut to crack.

From the very start of our basic education, the first thing that we were taught was 2+2=4

Now, here at this point time, we are more interested to know what are these ‘+’,’=’? These are operators.

Starting chronologically, we have three types of operators in C.

- Arithmetic Operator
- Logical Operator
- Bitwise Operator
- Assignment Operator

Operators | Syntax |
---|---|

Arithmetic Operators | * / % + – |

Logical Operators | && || ! |

Bitwise Operators | & | ^ ~ << >> |

& | ^ ~ << >> | x += -= %= *= /= &= |= <<= >>= |

The above-given table is just a brief overview of how the operators look like.

## Arithmetic Operators

The easiest part from which we all are acquainted from old times is arithmetic operators. It includes

- ‘+’: Addition
- ‘-‘: Subtraction
- ‘*’: Multiplication(asterisk)
- ‘/’: Division
- ‘%’: Remainder

Here we’ll check with the code, the functions of all these operators one by one.

```
#include <stdio.h>
int main()
{
int number1,number2;
printf("Enter the value of number1");
scanf("%d",&number1); //taking number 1 from the user
printf("Enter the value of number2");
scanf("%d",&number2); //taking number 2 from the user
printf("%dn",number1+number2);//you’ll see it adds the two numbers
printf("%dn",number1-number2);//it will give the difference between two numbers
printf("%dn",number1*number2);// it produces the product of two numbers
printf("%dn",number1/number2);//it gives the quotient
printf("%dn",number1%number2);//it delivers to you the remainder
return 0;
}
```

Let’s see what the output comes-

As you can see obviously, 7+2=9, 7-2=5, 7*2=14, 7/2=3(integral part), and when 7 is divided by 2 we get a remainder of 1. So, this was all about Arithmetic operators in C.

Now, next comes the logical operators.

## Logical Operators

Here we have the logical and (&&), logical or(||), and logical not(!).

### Logical And

Also called logical conjunction. The operator has a return type of Boolean. That is if anywhere this operator is used it gives true value in case all the applied condition is true or otherwise if even one of the conditions gets false it returns false. It is frequently used in codes wherein a particular line of code two or more different conditions should be satisfied at the same time.

An example of code will give you a clearer perspective-

Now, let’s understand this operator with the help of code-

```
//Here is a code of a particular situation where we have to check whether a student is promoted to next class or not so the student needs to satisfy two conditions, firstly, his should have more than 33 aggregate marks and secondly, he should have marks more than 90 in co-curriculars
#include <stdio.h>
int main()
{
int marks, cocurricular;
printf("Enter the aggregate marks ");
scanf("%d",&marks);
printf("Enter the grade in co-curricular");
scanf("%d",& ocurricular);
if(cocurricular>90 && marks>=33){
printf("PASS");
}
else{
printf("FAIL");
}
return 0;
}
```

Let’s see the output

### Logical OR

Next, talking about logical OR, it also has a return type of Boolean. If anywhere this operator is used it gives true value in case any of the applied conditions is true or otherwise if all the conditions get false it returns false. It is also frequently used in codes wherein a particular line of code two or more different conditions should be satisfied at the same time. So, the key point here is that if any case is TRUE the whole return is True.

Now, the third is logical NOT (!), it returns TRUE in case the condition is FALSE and gives FALSE if the condition to be tested was TRUE.

You’ll get all the concepts at your fingertips by looking at the below code and elucidation-

```
#include <stdio.h>
int main()
{
//Test yourself at each step
int a = 50, b = 60, c = 70, answer;
answer = (a != b) || (a <= b) || (a>c);//(50 != 60) || (50 <= 60) || (50>70)
printf("%dn",answer);
answer = !(a == b);//!(50 == 60)
printf("%dn",answer);
answer = !(a != b);//!(50 != 60)
printf("%dn",answer);
return 0;
}
```

Now, before explanation let’s take a look at the output-

Remember, // are used in C to write code comments so, read the code carefully twice before going to the explanation.

- (50 != 60) || (50 <= 60) || (50>70) – Talking about this statement first condition was 50!=60 which is obviously true as 50 is not equal to 60. 50<=60 is also true but the third condition is false as 50 is smaller than 70. So, since LOGICAL OR is used here so the basic requirement for the output to be TRUE(1) was that any one of the conditions gets True. So, the value of the answer is 1.
- !(50 == 60) – Talking about this statement 50==60 is a FALSE statement so the negation of FALSE (0) is TRUE(1).
- !(50 != 60) – Talking about this statement 50!=60 is a TRUE statement so the negation of TRUE (1) is FALSE(0).

## Bitwise Operator

The name itself tells that this operator operates in Bits. This function deals with Binary number Systems. The normal operands entered by the user are in the decimal number system. The BITWISE OPERATOR changes these operands to Binary before performing any action.

It composes of –

- BITWISE AND (&)
- BITWISE OR (|)
- BITWISE NOT (~)
- BITWISE LEFT SHIFT (<<)
- BITWISE RIGHT SHIFT (>>)
- BITWISE XOR (^)

Now, let’s understand this operator with the help of code-

```
#include <stdio.h>
int main()
{
unsigned char num1 = 3, num2 = 5;
printf("num1 = %d, num2 = %dn", num1, num2);
printf("num1&num2 = %dn", num1 & num2);
printf("num1|num2 = %dn", num1 | num2);
printf("num1^num2 = %dn", num1 ^ num2);
printf("~num1 = %dn", ~num1);
printf("num2<<1 = %dn", num2 << 1);
printf("num2>>1 = %dn", num2 >> 1);
return 0;
}
```

The output of the above code

### Explanation-

So we’ll start by analyzing the first line of the output

#### BITWISE AND

Binary of 3= 011 and of 5= 101

Now, Considering the AND truth table which goes as follows

Input 1 | Input 2 | Output |
---|---|---|

1 | 1 | 1 |

1 | 0 | 0 |

0 | 1 | 0 |

0 | 0 | 0 |

It is just like the Logical AND if even one of the values is 0 the whole outcome is 0.

So,

The second line of output

#### BITWISE OR-

Binary of 3= 011 and of 5= 101

Now, Considering the OR(|) truth table which goes as follows

Input 1 | Input 2 | Output |
---|---|---|

1 | 1 | 1 |

1 | 0 | 1 |

0 | 1 | 1 |

0 | 0 | 0 |

It is just like the Logical OR (||) if even one of the values is 1 the whole outcome is TRUE(1).

So,

The third line of Output

#### BITWISE XOR-

Binary of 3= 011 and of 5= 101

Now, Considering the XOR(^) truth table which goes as follows

Input 1 | Input 2 | Output |
---|---|---|

1 | 1 | 0 |

1 | 0 | 1 |

0 | 1 | 1 |

0 | 0 | 0 |

So,

The fourth line of Output

~3

This works as negation whenever there is 1 it converts it 0 and whenever there is 0 it converts it to 1.

So,

Fifth and Sixth lines of code

#### BITWISE LEFT SHIFT (<<)

It left shifts the number by the respective given number of bits. The bit positions that have been left vacant by the left shift operator are filled with 0.

#### BITWISE RIGHT SHIFT (>>)

It right shifts the number by the respective given number of bits. The bit positions that have been right vacant by the right shift operator are filled with 0.

## Assignment Operators

This is one of the operators type we are quite acquainted with it includes +=, -=, *=, /=, %=, >>=, <<=, |=, &=. Now, let us take a clearer perspective about how the assignment operator functions.

Carefully go through the below-written code.

```
#include <stdio.h>
int main()
{
int a = 10;
printf("Value of a is %dn", a);
a += 10;
printf("Value of a is %dn", a);
a -= 10;
printf("Value of a is %dn", a);
a *= 10;
printf("Value of a is %dn", a);
a /= 10;
printf("Value of a is %dn", a);
a %= 10;
printf("Value of a is %dn", a);
a &= 10;
printf("Value of a is %dn", a);
a |= 10;
printf("Value of a is %dn", a);
a >>= 10;
printf("Value of a is %dn", a);
a <<= 10;
printf("Value of a is %dn", a);
a /= 10;
printf("Value of a is %dn", a);
return 0;
}
```

Line 6: prints the original given value of the variable a.

Line 8: a+=10 is equivalent to a=a+10 that the new value of variable a becomes a=10+10

Line 10: a-=10 is equivalent to a=a-10 that the new value of variable a becomes a=20-10 (as the current value of a had become 20 in Line 8)

Line 12: a*=10 is equivalent to a=a*10 that the new value of variable a becomes a=10*10 (as the current value of a had become 10 in Line 1)

Line 14: a/=10 is equivalent to a=a/10 that the new value of variable a becomes a=100/10 (as the current value of a had become 100 in Line 10)

Line 16: a%=10 is equivalent to a=a%10 that the new value of variable a becomes a=10%10 =0 (as the current value of a had become 10 in Line 14)

Line 18: a&=10 is equivalent to a=a&10 that the new value of variable a becomes a=0&10 (as the current value of a had become 0 in Line 16)

### Calculation 1:

The binary value of 10 is 1010 and of 0 is 0

So, 1010 &0000 is 0000 that is a become 0.

Line 20: a|=10 is equivalent to a=a|10 that the new value of variable a becomes a=0|10 (as the current value of a had become 0 in Line 18)

### Calculation 2:

The binary value of 10 is 1010 and of 0 is 0

So, 1010 | 0000 is 1010 that is a become 10.

Line 22: a>>=10 is equivalent to a=a>>10 that the new value of variable a becomes a=10>>10 (as the current value of a had become 10 in Line 20)

### Calculation 3:

The binary value of 10 is 1010

So, 1010 >>10 is 0000 as 1010 right-shifted by 10 bits becomes 0.

Line 24: a<<=10 is equivalent to a=a<<10 that the new value of variable a becomes a=0<<10 (as the current value of a had become 0 in Line 22)

### Calculation 4:

So, 0000 >>10 is 0000 as 0000 right-shifted by 10 bits becomes 0.

So, fellows, these were all about operators in C. I hope I can make up the concept crystal clear.

Thank you for reading. You can ask questions that arise in your mind in the comment section.