### Online Classes

Results can only be achieved if you have the focused direction and crystal clear knowledge. To achieve this, you need a mentor. We will help you out by connecting with an expert mentor in the field.

Article

# Operators in C

10 min 40 sec

Operators in C programming are used to perform the operations on variables. Most common operations include addition, subtraction, multiplication, division, modular division, etc.

Types of Operators in C

Operators Description
*  /  %  +  -  Arithmetic Operators
!= > < >= <= ==/td>  Relational Operators
&& || !  Logical Operators
&, |, ^, ~, >> <<  Bitwise Operators
+=, -=, *=,=  Assignment Operators
conditional, comma, sizeof, address, redirecton   Other Operators

We will explore each type of operator available in C in this tutorial. So let/'s begin with the Arithmetic Operators first.

## Arithmetic Operators

Arithmetic Operators are  basically used to perform the general addition, subtraction, multiplication, division, and modular division operations. The following table includes all the Arithmetic Operators in C as follows,

Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)

The following program illustrates how to use the arithmetic operators in C,

```// Working of arithmetic operators
#include  <stdio.h>
int main()
{
int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d
",c);
c = a-b;
printf("a-b = %d
",c);
c = a*b;
printf("a*b = %d
",c);
c = a/b;
printf("a/b = %d
",c);
c = a%b;
printf("Remainder when a divided by b = %d
",c);

return 0;
}
```

Output:

```a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b = 1```

The +, -, *, / operators are used to compute the basic calculations addition, subtraction, division, and multiplication.

Modulo operator(%) is used to give the remainder in the division operation.

## Bitwise Operators in C

There are many Bitwise operators in C as defined below:

& (bitwise AND):- Performs the AND Operation bit by bit. The result of AND is 1 only if both bits are 1.

| (bitwise OR):- Performs the OR Operation bit by bit. The result of OR is 1 any of the two bits is 1.

^ (bitwise XOR):- Performs the XOR Operation bit by bit. The result of XOR is 1 if the two bits are different.

<< (left shift):- Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

>> (right shift):- Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

~ (bitwise NOT):- Takes one number and inverts all bits of it.

Visit Bitwise Operators in C topic for detailed reference.

## Logical Operators in C

An expression containing a logical operator returns either 0 or 1 depending upon whether the expression results in true or false. The following table represents different types of logical operators in C programming as follows,

Operator Meaning
&& Logical AND. True only if all operands are true
|| Logical OR. True only if either one operand is true
! Logical NOT. True only if the operand is 0

C Program to illustrate the concept of Logical Operators in C

```#include <stdio.h>

int main()
{
int marks = 65;
if(marks > 75  && marks < 99)
{
printf("He is topper");
}
else if(marks < 75 && marks >= 65)
{
printf("He cracked the exam");
}
else
{
printf("He failed");
}
return 0;
}
```

Output:

`He cracked the exam`

## Assignment Operators in C

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

Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

C program to illustrate the concept of Assignment Operators

```#include  <stdio.h>
int main()
{
int a;
a+= 10;
printf("a = %d
",a);

a -= 2;
printf("a = %d
",a);

a *= 2;
printf("a = %d
",a);

a /= 2;
printf("a = %d
",a);

a %=2;
printf("a = %d
",a);
return 0;
}```

Output

```a = 10
a = 8
a = 16
a = 8
a = 0```

## Postfix Increment

```int i = 10;
printf("%d", i++);
printf(“
%d", i);
```

## Output :-

```10
11
```

Here,

i++ is postfix operator because ++ is used after a unary operand. Unary means one operand which is i in this given example. In postfix case , value is assigned to i at first which is 10 and then after that whenever i is being used its value will be incremented by 1 when ++ is used .

## Prefix Increment

```int i = 10;
printf("%d", ++i);
printf(“
%d", i);
```

## Output :-

```11
11
```

Here,

++i is prefix operator because ++ is used before a unary operand. Unary means one operand which is i in this given example. In prefix case, the value is incremented by 1 at first and then from that point value of i is being changed to incremented value.

## Postfix Decrement

```int i = 10;
printf("%d", i--);
printf(“
%d", i);
```

## Output :-

```10
9
```

Here,

i-- is postfix operator because -- is used after a unary operand. Unary means one operand which is i in this given example. In postfix case , value is assigned to i first which is 10 and then after that whenever i is being used its value will be decremented by 1 when -- is used.

## Prefix Decrement :-

```int i = 10;
printf("%d", --i);
printf(“
%d", i);
```

## Output :-

```9
9
```

Here,

--i is prefix operator because -- is used before a unary operand. Unary means one operand which is i in this given example. In prefix case , value is decremented by 1 and then from that point value of i is being changed to decremented value when -- is used .

## Example

```a = 10;
Output
a++         =     10 ++           =  10
a++         =     11++            =  11
a++         =     12++            =  12
a           =     13              =  13

++a         =     ++10            = 11
++a         =     ++11            = 12
++a         =     ++12            = 13
a           =      13             = 13
```

As discussed,

## Remember this rule

In postfix case, the value is assigned to i at first which is 10 and then after that whenever i is being used its value will be incremented by 1 when ++ is used .

a++ will use the original value which is 10 at first and then in the second step whenever a is used its value will be incremented so in second step value will become 11 which is then incremented in third step so its value becomes 12 in the third step which is then incremented so whenever its used in fourth step its value will be incremented by 1.

In prefix case, the value is incremented by 1 at first and then from that point value of i is being changed to incremented value.

++a increments the value directly and use the incremented value afterward.

## Example

```a = 10;
Output
a--         =     10--           =  10
a--         =     9--            =  9
a--         =     8--            =  8
a           =     7              =  7

--a         =     --10           =  9
--a         =     --9            =  8
--a         =     --8            =  7
a           =       7            =  7

```

## Hierarchy of Operations

There are various scenarios where the compiler has to know which operation should perform first. What do you think this expression will be evaluated according to the compiler?

`2 * x - 3 * y `

Now,

There are two ways to solve this equation

```1)  (2x)-(3y)
2)  2(x-3y)
```

We engineers know how to solve these problems because we know multiplication is solved first then minus operation should be applied. But how does the compiler know how to solve this? So Hierarchy of Operations helps the compiler to understand this kind of equations.

## The following table list the operators according to priorities:-

Priority Operators Description
1st */% multiplication, division, modular division
2nd +- addition, subtraction
3rd = assignment

## What this table signifies?

* will be always solved first then division (/) operator. + will be solved before - operator but * will be solved first then + operator.

## Example

`a = 2*3/4+4/4`

As * has max priority than '/' So it will be evaluated first. So,

`6/4+4/4`

Now '/' has max priority than '+' so

`1 + 4/4`

Now '/' has max priority than '+' so

`1 + 1`

Result is 2

## Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

## HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor