Programs

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.

Every operator has its subtypes and branchings. 

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

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

OPERATOR OPERAND

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;

}

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

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

3. Assignment Operators

We use these operators to assign specific values to variables.

OPERATOR

NAME USE

=

Assignment

Assigns value from right to left operand

+=

Addition assignment

Stores summed value in the left operand

-=

Subtraction assignment Stores subtracted value in the left operand

*=

Multiplication assignment Stores 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

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);

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.

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

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?

Plan Your Software Development Career Now.

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks