A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023

Article by: Manish Methani

Last Updated: November 7, 2021 at 2:04pm IST
7 min 19 sec

Operators are an essential part of any programming language, and C programming language is no exception. Operators are symbols that represent a specific operation that can be performed on one or more operands. In C, operators are classified into different types based on their functionality. In this tutorial, we will explore everything about operators in the C programming language with examples.

Types of Operators in C

Arithmetic Operators

Arithmetic operators are used for mathematical calculations. The following are the arithmetic operators in C:

• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulo (%)

Here is an example of using arithmetic operators in C:

```#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```

Relational Operators

Relational operators are used to comparing two values. The following are the relational operators in C:

• Greater than (>)
• Less than (<)
• Greater than or equal to (>=)
• Less than or equal to (<=)
• Equal to (==)
• Not equal to (!=)

Here is an example of using relational operators in C:

```#include <stdio.h>
int main() {
int a = 10, b = 4;
printf("a > b is %d", a > b);
printf("a < b is %d", a < b);
printf("a >= b is %d", a >= b);
printf("a <= b is %d", a <= b);
printf("a == b is %d", a == b);
printf("a != b is %d", a != b);
return 0;
}```

Output:

```a > b is 1
a < b is 0
a >= b is 1
a <= b is 0
a == b is 0
a != b is 1```

Logical Operators

Logical operators are used to combining two or more conditions. The following are the logical operators in C:

• Logical AND (&&)
• Logical OR (||)
• Logical NOT (!)

Here is an example of using 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`

Bitwise Operators

Bitwise operators are used to perform bitwise operations on two integer operands. The following are the bitwise operators in C:

• Bitwise AND (&)
• Bitwise OR (|)
• Bitwise XOR (^)
• Bitwise NOT (~)
• Left shift (<<)
• Right shift (>>)

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.

Here is an example of using bitwise operators in C:

```#include <stdio.h>
int main() {
int a = 5, b = 9, result;
result = a & b;
printf("Bitwise AND: %d
", result);
result = a | b;
}

```

Output:

`Bitwise AND: 1`

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```

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
3rd = assignment

What this table signifies?

* will be always solved first then the division (/) operator. + will be solved before the - operator but * will be solved first then the + 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

Learn Flutter, FlutterFlow, Firebase & Angular.

Looking to learn how to create the production-ready apps
from scratch? Well, you are at the right place.

₹1299 or \$15.56

₹1299 or \$15.56

₹1299 or \$15.56

₹1299 or \$15.56

FREE

FREE

FREE

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.