Different Types of Operators Explained with Examples | upGrad blog (2024)

Home>Software Development>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 ourfree courses to get an edge over the competition

Every operator has its subtypes and branchings.

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

Table of Contents

What are the types of operators in C?

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

Different Types of Operators Explained with Examples | upGrad blog (1)

  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:

Different Types of Operators Explained with Examples | upGrad blog (2)

  • *(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.

Suppose you are looking to master C and develop applications like Swiggy, IMDB, etc.. In that case, upGrad’s 13-monthExecutive PG Programme in Software Development – Specialisation in Full Stack Developmentcan 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.

What 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.

How 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.

What 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.

Want to share this article?

Different Types of Operators Explained with Examples | upGrad blog (3)

Plan Your Software Development Career Now.

Apply Now for Master of Computer Science from Liverpool John Moores University

Different Types of Operators Explained with Examples | upGrad blog (2024)

FAQs

What are the different types of operators explain with examples? ›

There are three types of operator that programmers use: arithmetic operators. relational operators. logical operators.
...
Arithmetic operators.
Arithmetic operationOperatorExample
Addition+x = x + 5
Subtraction-x = x - 5
Multiplication*x = x * 5
Division/x = x / 5
2 more rows

What are operators name the different types of operators with examples? ›

  • Arithmetic Operators. An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. ...
  • Relational operators. These operators are used to compare values and always result in boolean value (True or False). ...
  • Logical Operators. ...
  • Bitwise Operators. ...
  • Assignment Operators. ...
  • Misc.
21 Aug 2022

What are different types of operators in DBMS explain? ›

SQL Arithmetic Operators
OperatorDescription
- (Subtraction)Subtracts right hand operand from left hand operand.
* (Multiplication)Multiplies values on either side of the operator.
/ (Division)Divides left hand operand by right hand operand.
% (Modulus)Divides left hand operand by right hand operand and returns remainder.
1 more row

What are the different types of operators explain with examples Class 7? ›

The following are the Arithmetic Operators in C and C++: + (Addition) – This operator adds two operands together,-(Subtraction) – Remove two operands from the equation , * (Multiplication) – Add two operands together, / (Division) – Divides two operands and returns the result as the quotient , percent (Modulus ...

What are the seven 7 types of operator? ›

The different types of operators are arithmetic operators, assignment operators, comparison operators, logical operators, identity operators, membership operators, and boolean operators.

What is an operator explain? ›

In mathematics and computer programming, an operator is a character that represents a specific mathematical or logical action or process. For instance, "x" is an arithmetic operator that indicates multiplication, while "&&" is a logical operator representing the logical AND function in programming.

What are the 4 types of arithmetic operators? ›

Addition (Finding the Sum; '+') Subtraction (Finding the difference; '-') Multiplication (Finding the product; '×' ) Division (Finding the quotient; '÷')

What is operator give few examples of operators? ›

Techopedia Explains Operator

A variable is the framework of the information. Arithmetic Operators: These include "+" (addition), "-" (subtraction), "*" (multiplication), "/" (division), "\" (integer division), "Mod" (Modulo) and "^" (exponentiation).

What are SQL operators explain with example? ›

SQL Logical Operators
OperatorDescriptionExample
ANYTRUE if any of the subquery values meet the conditionTry it
BETWEENTRUE if the operand is within the range of comparisonsTry it
EXISTSTRUE if the subquery returns one or more recordsTry it
INTRUE if the operand is equal to one of a list of expressionsTry it
6 more rows

What are different types of operators in SQL? ›

What is the Precedence of SQL Operator?
SQL Operator SymbolsOperators
+, -Identity operator, Negation operator
*, /Multiplication operator, Division operator
+, -, ||Addition (plus) operator, subtraction (minus) operator, String Concatenation operator
=, !=, <, >, <=, >=, IS NULL, LIKE, BETWEEN, INComparison Operators
4 more rows

What are the three types of operators and describe each type? ›

​ - Brainly.in.
...
Answer:
  • Arithmetic Operators. It includes basic arithmetic operations like addition, subtraction, multiplication, division, modulus operations, increment, and decrement.
  • Relational Operators. ...
  • Logical Operators.
7 Oct 2020

What are the different types of operators for Class 11? ›

There are two types of operators: unary and binary. Unary operator operates only on one operand, such as negation. On the other hand, binary operator operates on two operands, which includes addition, subtraction, multiplication, division, exponentiation operators etc.

What are examples of text operators? ›

On this page: & (Ampersand)Operator. && (Double Ampersand) Operator.
...
& (Ampersand) Operator
  • boldface: Indicates words and characters that must be typed exactly.
  • italic: Indicates expressions or other variable elements.
  • {} (curly braces): Indicate optional elements.
19 Aug 2021

What are the 8 types of operators? ›

Broadly, there are eight types of operators in C and C++. They are:
  • Increment and decrement operators.
  • Bitwise operators.
  • Assignment operators.
  • Logical operators.
  • Relational operators.
  • Special operators.
  • Conditional operators.
  • Arithmetic Operators.
22 Oct 2021

What are Boolean operators? ›

Boolean Operators are simple words (AND, OR, NOT or AND NOT) used as conjunctions to combine or exclude keywords in a search, resulting in more focused and productive results. This should save time and effort by eliminating inappropriate hits that must be scanned before discarding.

What are the different logical operators? ›

There are three logical operators: and , or , and not . The semantics (meaning) of these operators is similar to their meaning in English.

What are the 8 types of operators in C? ›

Explanation of Operators in C
  • Arithmetic Operators. ...
  • Relational Operators. ...
  • Logical Operators. ...
  • Bitwise Operators. ...
  • Assignment Operators. ...
  • Conditional Operators. ...
  • Special Operators.

What is DDL DML and DCL in DBMS? ›

Within SQL are three main sub-languages that are used to manipulate and manage both the database and data itself: DCL, DDL, and DML. DCL stands for data control language. DDL stands for data definition language. DML stands for data manipulation language.

How many basic operators are there? ›

The following are the five types of binary operators used in computer programming: Arithmetic.
...
Binary operators.
Operator+=
Definitionaddition and assignment
Syntaxx += y
OperationAdds the right operand to the left operand and assigns the result to the left operand
Examplex = 5, y = 2 after x += y x = x+y x = 7
5 more columns

How many types of operations are there? ›

There are two common types of operations: unary and binary. Unary operations involve only one value, such as negation and trigonometric functions. Binary operations, on the other hand, take two values, and include addition, subtraction, multiplication, division, and exponentiation.

What is operator and different type of operator? ›

Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while - is an operator used for subtraction. Operators in C++ can be classified into 6 types: Arithmetic Operators.

What are operators name the different types of operators with examples Class 6? ›

Answer: Arithmetic Operators: These are the operators used to perform arithmetic/mathematical operations on operands. Examples: (+, -, *, /, %,++,–). Arithmetic operator are of two types:Unary Operators: Operators that operates or works with a single operand are unary operators.

What are the four different types operators? ›

Types of operators

There are four different types of calculation operators: arithmetic, comparison, text concatenation, and reference.

What are operators give any two examples? ›

Arithmetic Operators
OperatorDescriptionExample
+Adds two operandsA + B will give 30
-Subtracts second operand from the firstA - B will give -10
*Multiplies both operandsA * B will give 200
/Divides numerator by de-numeratorB / A will give 2
1 more row

What are operators define? ›

noun. a person who operates a machine, apparatus, or the like: a telegraph operator. a person who operates a telephone switchboard, especially for a telephone company.

Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 6221

Rating: 4.8 / 5 (58 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.