**Article by:** Manish Methani

**Last Updated:** November 7, 2021 at 2:04pm IST

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.

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

- Addition (+)
- 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 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 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 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

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

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.

Priority | Operators | Description |
---|---|---|

1st | */% | multiplication, division, modular division |

2nd | +- | addition, subtraction |

3rd | = | assignment |

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

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

10 OS Interview Questions you must know in 2024

10 Angular Interview Questions you must know in 2024

10 Javascript Interview Questions you must know in 2024

Top 10 DSA Interview Questions You should not miss

Top 10 Web Development Interview Questions

Introduction to Next.js

Altina Schinasi: Celebrating the Visionary Designer of Cat-Eye Frames | Google Doodle Tribute

Best Niche for Blogging in USA: A Comprehensive Guide

18 Must-Do Activities for Computer Science Students in College

13 Essential Things Solopreneurs need to know in 2024

25 Practical ways to earn money online

The Power of Gratitude - A Journey to Personal Growth

Top 5 Technology Trends That Are Shaping Our Future

5 Books You Need to Read Right Now

How to answer tell me about yourself?

What Angular feature transforms data before displaying it in the view?

Which widget is used in Flutter to create a button?

6 Mobile App Ideas That You Can Start in 2024

Flutter Widget Showdown: Exploring the Developers Favorite for Layouts!

Mobile App

For Engineers.

Mock Tests.

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

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

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

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

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

Take Test