Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconDifferent Types of Operators Explained with Examples

Different Types of Operators Explained with Examples

Last updated:
22nd Oct, 2021
Views
Read Time
9 Mins
share image icon
In this article
Chevron in toc
View All
Different Types of Operators Explained with Examples

Learning programming languages begins with coding fundamental mathematical problems that require basic mathematical operations. This includes conditional, logical, bitwise, and arithmetic mathematical operations. This is accomplished by using operators in programming languages. Operators are fundamental tools or signs that help us perform mathematical and logical operations most only.

Programming languages are used to resolve real-time issues using technology. And operators are an integral tool required for programming or coding, irrespective of the language used.

Check out our free courses to get an edge over the competition

Every operator has its subtypes and branchings. 

Ads of upGrad blog

In this article, we’ll look at the types of operators in C. 

What are the types of operators in C?

Broadly, there are eight types of operators in C and C++. They are: 

  1. Increment and decrement operators
  2. Bitwise operators
  3. Assignment operators
  4. Logical operators
  5. Relational operators
  6. Special operators
  7. Conditional operators
  8. Arithmetic Operators

Check out upGrad’s Java Bootcamp

Let’s understand each of these in detail:

1. Arithmetic Operators

These operators help perform primary arithmetic operations like multiplying, dividing, adding, subtracting, finding modulus, etc.

NAME

OPERATOROPERAND

OPERATION

Addition

+x,y

x+y; adds two numbers

Subtraction

x,y

x-y; subtracts one number from another

Multiplication

*x,y

x*y; returns the product of two numbers

Division

/x,y

x/y; returns the quotient when two numbers are divided

Modulus

%x,y

x%y; returns the remainder when two numbers are divided

EXAMPLE CODE:

#include <iostream>

using namespace std;

int main()

{

int k= 22, b = 4;

cout<<“Addition of “<< k << ” and ” << b << ” is ” << k + b <<endl;

cout<<“Subtraction of “<< k << ” and ” << b << ” is: ” << k – b <<endl;

cout<<“Multiplication of “<< k << ” and ” << b << ” is: ” << k * b <<endl;

cout<<“Division of “<< k << ” and ” << b << ” is: ” << k / b <<endl;

cout<<“Modulus between “<< k << ” and ” << b << ” is: ” << k % b <<endl;

cout<<“Incremented value ++k is: “<< ++k <<endl;

cout<<“Decremented value –k is: “<< –k <<endl;

return 0;

}

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN) 

OUTPUT:

The addition of 22 and 4 is: 26

Subtraction of 22 and 4 is: 18

Multiplication of 22 and 4 is: 88

Division of 22 and 4 is: 5

Modulus between 22 and 4 is: 2

Incremented value ++k is: 23

Decremented value –k is: 22

Explore Our Software Development Free Courses

2. Decrement and Increment Operators

These operators are useful in minimizing calculation. n=n+1 can be truncated to n++. The operators are:

  1. Increment (++)
  2. Decrement (–)

There is a significant difference in usage of the operators depending on the place of application.

  1. Pre-increment operators: if we write the ++ operator before the variable name, one is added to the operand, and after that, the result is assigned to the variable.
  2. Post-increment operators: if we write the ++ operator after the variable name, the value is first assigned to the variable, and increment by 1 occurs.

The same happens for pre-decrement and post-decrement operators.

 EXAMPLE CODE:

#include <stdio.h>

void main()

{

   int a1=7, b1=7;

printf(“\n%d %d”,a1–,–b1);

printf(“\n%d %d”,a1–,–b1);

printf(“\n%d %d”,a1–,–b1);

printf(“\n%d %d”,a1–,–b1);

printf(“\n%d %d”,a1–,–b1);

}

OUTPUT:

7 6

6 5

5 4

4 3

3 2

Explore our Popular Software Engineering Courses

3. Assignment Operators

We use these operators to assign specific values to variables.

OPERATOR

NAMEUSE

=

Assignment

Assigns value from right to left operand

+=

Addition assignment

Stores summed value in the left operand

-=

Subtraction assignmentStores subtracted value in the left operand

*=

Multiplication assignmentStores multiplied value in the left operand

/=

Division assignment

Stores quotient in the left operand

%=Modulus assignment

Stores remainder in the left operand

4. Relational Operators

Relational operators are used for comparing two values of quantities with each other. It establishes a relation between two values.

Note: In programming languages like C or C++, we use two ‘=’ (==) to check the equality, as one ‘=’ (=) sign is used as an assignment operator. We use six types of relational operators:

NAME

OPERATOR

USAGE

Equal to

==Checks whether the two operand values are equal

Not equal to

!=Checks whether the two operand variables or constants are not equal to each other

Lesser than equal to

<=Checks if one value is lesser than or equal to the other one

Greater than equal to

>=

Checks if one of the values is greater than or equal to another one
Lesser than

Checks whether one operand is lesser than the other one

Greater than

Checks whether one parent is greater than the other one

EXAMPLE CODE:

#include <iostream>

using namespace std;

int main()

{

int q = 10, w = 10, e = 20;

cout<<“For ” << q << ” == ” << w << ” the result is: ” << (q == w) << endl;

cout<<“For ” << q << ” == ” << e << ” the result is: ” << (q == e) << endl;

cout<<“For ” << q << ” != ” << e << ” the result is: ” << (q != e) << endl;

cout<<“For ” << q << ” != ” << w << ” the result is: ” << (q != w) << endl;

cout<<“For ” << q << ” > ” << w << ” the result is: ” << (q > w) << endl;

cout<<“For ” << q << ” > ” << e << ” the result is: ” << (q > e) << endl;

cout<<“For ” << q << ” < ” << w << ” the result is: ” << (q < w) << endl;

cout<<“For ” << q << ” < ” << e << ” the result is: ” << (q < e) << endl;

cout<<“For ” << q << ” >= ” << w << ” the result is: ” << (q >= w) << endl;

cout<<“For ” << q << ” >= ” << e << ” the result is: ” << (q >= e) << endl;

cout<<“For ” << q << ” <= ” << w << ” the result is: ” << (w <= w) << endl;

cout<<“For ” << q << ” <= ” << e << ” the result is: ” << (q <= e) << endl;

return 0;

} 

OUTPUT:

For 10==10 the result is: 1

For 10==20 the result is: 0

For 10!=20 the result is: 1

For 10!=10 the result is: 0

 

For 10>10 the result is: 0

For 10>20 the result is: 0

For 10<10 the result is: 0

For 10<20 the result is: 1

 

For 10>=10 the result is: 1

For 10>=20 the result is: 0

For 10<=10 the result is: 1

For 10<=20 the result is: 1

In-Demand Software Development Skills

5. Logical Operators

We use six logical operators when we need to make decisions by testing one or more conditions. Thus, logical operators work on Boolean values. The answers returned are either true or false.

Logical operators are of 2 types:

  1. Unary operators: These work with one variable.
  2. Binary operators: These work with two variables.

Unary operators in C

Operators that work on one variable to decide on a result are known as Unary operators.

  • Operator: ! (NOT)

The NOT operator issues negation on a constant or variable – Used as (!a)

Binary operators in C

Operators that work with two variables are called binary operators. The evaluated result is based on both of them individually.

The two binary operators in c/c++ are:

  • && : (AND) logical conjunction of expressions.

It checks whether both the opponents are actual – Used as (a&&b)

  • || : (OR) logical disjunction of expressions.

It checks if either one of the operands is true or not – Used as (a||b)

EXAMPLE CODE:

#include <stdio.h>

int main()

{

int m = 10, n= 10, c = 20, answer;

printf(“Logical operator example: \n\n”);

answer = (m == n) && (c > n);

printf(“For (%d == %d) && (%d != %d), the output is: %d \n”,m,n,n,c,answer);

answer = (m == n) && (c < n) && (c>0);

printf(“For (%d == %d) && (%d <= %d), the output is: %d \n”,m,n,n,c,answer);

answer = (m == n) || (n > c);

printf(“For (%d == %d) || (%d < %d), the output is: %d \n”,m,n,c,n,answer); 

answer = (m != n) || (m <= n) || (m>c);

printf(“For (%d != %d) || (%d < %d), the output is: %d \n”,m,n,c,n,answer);

answer = !(m == n);

printf(“For !(%d == %d), the output is: %d \n”,m,n,answer);

answer = !(m!= b);

printf(“For !(%d == %d), the output is: %d \n”,m,n,answer);

return 0;

}

OUTPUT:

For (10==10) && (10!=20), the output is: 1

For (10==10) && (10<=20), the output is: 1

For (10==10) || (20<10), the output is: 1

For (10!=10) || (10!=20), the output is: 1

For !(10==10), the output is: 0

For !(10==10), the output is: 1

6. Conditional Operator

Conditional operator or ternary operator reduces the work of an if-else block 21 single statement. It is constructed for conditional expressions.

Syntax:

VariableName = (condition) ? TrueValue : FalseValue;

Example:

a= (b>c) ? (b+c) : (b-c);


upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

 

7. Bitwise Operators

Bitwise operators perform based on Boolean algebra. These operators boost the efficiency of a program exponentially by increasing the processing speed of programs. 

  • Bitwise AND: converts the two operands into binary and performs conjunctive operation bit by bit.
  • Bitwise OR: converts the two operands into binary and performs disjunctive operation bit by bit.
  • Bitwise LEFT SHIFT:
  • Bitwise RIGHT SHIFT:
  • Bitwise XOR: converts both operands into binary and performs xor operation bit by bit
  • Bitwise ONE’S COMPLEMENT: returns the complementary form of the operand.

Bitwise operators do not work for float or double data types in C.

8. Special Operators

C/C++ facilitates the usage of some special operators, which helps in reducing the hassle of programmers. Some of them are:

  • *(Pointer)= it stores the memory address of a variable.
  • &(Pointer)= this points to the memory location where the computer stores the operand.
  • sizeof= this operator returns the space occupied by a particular data type in its memory location.

Read our Popular Articles related to Software Development

Conclusion

Operators and their functioning form the fundamentals of any programming language. To write complex code for running different apps or software, one must have a crystal-clear understanding of operators. Thus, having an in-depth understanding of their usage is crucial for aspirants who wish to excel at coding.

Ads of upGrad blog

Suppose you are looking to master C and develop applications like Swiggy, IMDB, etc.. In that case, upGrad’s 13-month Executive PG Programme in Software Development – Specialisation in Full Stack Development can kickstart your learning journey. Offered by IIIT Bangalore, the course includes nine projects and assignments and a coveted software career transition boot camp for non-tech and new coders. In addition to the comprehensive syllabus taught by world-class faculties, the program also includes upGrad’s 360° Career Support, where students are exposed to preparation material, mock interviews, and job fairs to increase their chances of recruitment.

Reach out to us today to get started! 

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What is the difference between ‘=’ and ‘==’ operators?

In programming languages like C or C++, we use '==' to check the equality, whereas '=' sign is used as an assignment operator. ‘A=5+2;’ means assigning 7 to the variable A. On the other hand, ‘if(A==5)’ checks whether the value assigned to the variable is 5 or not.

2How do pre-increment and post-increment operators work?

1. Pre-increment operators: Writing ++ before the variable increments the value be 1 and assigns the new valve to the variable.

2. Post-increment operators: Writing ++ after the variable assigns the value to the variable first and then increments it by 1.

3What do the special operators do?

The special operators and their uses are:

1. *: To store memory location
2. &: To return memory location.
3. sizeof: To return the space occupied by a particular data type in its memory location.

Explore Free Courses

Suggested Blogs

Best Jobs in IT without coding
134217
If you are someone who dreams of getting into the IT industry but doesn’t have a passion for learning programming, then it’s OKAY! Let me
Read More

by Sriram

12 Apr 2024

Scrum Master Salary in India: For Freshers &#038; Experienced [2023]
900302
Wondering what is the range of Scrum Master salary in India? Have you ever watched a game of rugby? Whether your answer is a yes or a no, you might h
Read More

by Rohan Vats

05 Mar 2024

SDE Developer Salary in India: For Freshers &#038; Experienced [2024]
905039
A Software Development Engineer (SDE) is responsible for creating cross-platform applications and software systems, applying the principles of compute
Read More

by Rohan Vats

05 Mar 2024

System Calls in OS: Different types explained
5021
Ever wondered how your computer knows to save a file or display a webpage when you click a button? All thanks to system calls – the secret messengers
Read More

by Prateek Singh

29 Feb 2024

Marquee Tag &#038; Attributes in HTML: Features, Uses, Examples
5131
In my journey as a web developer, one HTML element that has consistently sparked both curiosity and creativity is the venerable Marquee tag. As I delv
Read More

by venkatesh Rajanala

29 Feb 2024

What is Coding? Uses of Coding for Software Engineer in 2024
5051
Introduction  The word “coding” has moved beyond its technical definition in today’s digital age and is now considered an essential ability in
Read More

by Harish K

29 Feb 2024

Functions of Operating System: Features, Uses, Types
5123
The operating system (OS) stands as a crucial component that facilitates the interaction between software and hardware in computer systems. It serves
Read More

by Geetika Mathur

29 Feb 2024

What is Information Technology? Definition and Examples
5057
Information technology includes every digital action that happens within an organization. Everything from running software on your system and organizi
Read More

by spandita hati

29 Feb 2024

50 Networking Interview Questions &#038; Answers (Freshers &#038; Experienced)
5133
In the vast landscape of technology, computer networks serve as the vital infrastructure that underpins modern connectivity.  Understanding the core p
Read More

by Harish K

29 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon