Operators In C++ | How Operators Play In C++?

Our program does some operations to provide us with results or output. The program does it’s operations through use of operators. The entities on which operators act are called operands.

There are different types of operators in C++. Some of the basic types of operators are following:

  1. Arithmetic operator: 
    1. Binary operator
    2. Unary operator
  2. Relational operator
  3. Logical operator
  4. Bitwise Operator
  5. Assignment operators
  6. Miscellaneous operator

For a better understanding, look at the diagram below.

Operators In C 1
Operators In C++

Let’s know some basic information about these operators.

Arithmetic operator | Binary Operator In C++:

These are the operators which act upon 2 operands. Binary operators are +(used for addition), -(used to subtract), *(used to multiply), /(used for division) and %(used to get the remainder).

An example program of arithmetic operator:

#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
  int a = 100;
  int b = 20;
  int c = 15;
  int d = 44;
  int result;
 
  result = a - b; 
  cout <<"\na - b = "<<result;
 
  result = b * c; 
  cout <<"\nb * c = "<< result;
 
  result = a / c; 
  cout <<"\na / c = "<< result; 
 
  result = a + b * c; 
  cout <<"\na + b * c = "<< result;
 
  return 0;
}

tbfGbueXTgLyQo8YHwwV7TSZSnqvb0qX l3JmCRjQdy7pnVd9hO5 Djv0yRSK

Credit Goes To Shiva Patra

Unary Operator In C++:

These are the operators who act upon only one operand. There are two unary operators ++(Increment operator) to increase the value of operand by one, –(Decrement operator) to decrease the value of operand one. There are two kinds of Increment(++) and Decrement(–) operators. One is post and another one is pre-incrementer, their operation depends on the position they are placed in.

Post increment(x++) In C++:

The value of the variable will be preserved temporarily and before the execution of the next statement, the value will be incremented.

For example:

#include<iostream>
using namespace std;
int main(){
int x=3;
x=x++;
cout<<x<<endl;
return 0;
}

E0T6tdtvavwXOpvxBTjIZjqv LZsTxceLRzbrtcsjuVp84qWR8aWXMlfXwzh8pgjIC3R1S3afCqzjmrFkta7X9zzzTTzIC9z5wRePfuPYyus8rZYYUK31L 6wUVuvLBR29A3g

Credit Goes To Shiva Patra

In the above program the value of variable(x) remains the same as it will be incremented before the execution of the next statement.

Pre increment(++x) In C++:

The value of the variable is incremented instantly.

For Example:

#include<iostream>
using namespace std;
int main(){
int x=3;
x=++x;
cout<<x<<endl;
return 0;
}

Credit Goes To Shiva Patra

Here, the value of variable(x) incremented instantly to 4. To understand better let’s take one more example:

#include<iostream>
using namespace std;
int main(){
int x=3;
x= x++ + ++x;
cout<<x<<endl;
return 0;
} 

oKL1SFSliX3V9omsFXL9oGHL3DY KCsNjUJ7G6nupnf z Pvd1q71xWumF4RcM4dgh2LTpfuZ2Ca0n Q0YtIP2uVIwijAUzJpKDiwUl5xaNZ7tjJyxsTrRr9AMTL58o WkHiA

Credit Goes To Shiva Patra

In the above program the value of x is preserved as 3 and before pre increment the value of x is changed to 4 and then, it is incremented to 5 because of pre increment. So, 3 is added to 5 which results in 8.

Relational Operator In C++:

It defines the relationship between the two operands and returns a boolean value. Different Relational Operators are following:

  • == => If both operands have equal value, it gives true value.
  • != => If both operands are not equal, it gives true.
  • > => If the left operand is more than the right operand, it gives true.
  • < => It is true if the right operand is more than the left operand. >= It is true if the left operand is greater or equal to the right operand.
  • <= => It gives true when the right operand is greater or equal to the left operand.

So, these operators are used in programs something like the example given below:

#include <iostream> 
using namespace std; 
int main() { 
int x = 4; 
int y = 6; 	
if (x > y) 
{ 
	cout << "x > y is not true" << 
			endl; 
} 	
if (x < y) 
{ 
	cout << "x < y is true" << endl; 
} 
if (x == y) 
{ 
	cout << "x==y is not equal to true" << 
			endl; 
} 
if (x != y) 
{ 
	cout << "x != y is true" << 
			endl; 
}	
if (x >= y) 
{ 
	cout << "x >= y is not true" << 
			endl; 
} 	
if (x <= y) 
{ 
	cout << "x <= y is true" << 
			endl; 
} 
return 0; 
}

3XdcG10TPy8fS GqQpKanfQ6a8dtubNb82o90BV8 szeK4G7S

Credit Goes To Shiva Patra

Logical operator:

These are used to connect multiple conditions/expressions together or to reverse logical value or simple words they are used to find logic between two variables or values. So, the logical operators in C++ are:

  • && This is AND operator, only gives true if both the operands are true.
  • || It’s called OR operator, it gives true if at least one of the operands is true.
  • ! It is NOT an operator, it gives the opposite logical value of the operand means it turns true value to false and vice-versa.

Logical operators can be used in programs like the below mentioned example:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int x=10, y=8,z=12,d=14;

  if(!(x==0))
     cout<<"x is not zero"<<endl;
  else
    cout<<"x is zero"<<endl; if((x>y)&&(z<d))
        cout<<"Logicxl AND is true"<<endl;
  else
       cout<<"Logicxl AND is false"<<endl;

 if((x<z)||(y<d))
     cout<<"Logicxl OR is true"<<endl;
 else
     cout<<"Logicxl OR is false"<<endl;
}

L74FO2okCuxdwDbbbHdZRluUBxJ1E45EYPl0qsJdZaSxD9FMmVnpOLr4EwnwcRogj

Credit Goes To Shiva Patra

Bitwise Operator In C++:

These operators act upon bits individually (bit by bit). Bitwise operators in C++ are & (AND), |(OR), ^ (XOR), ~ (ones complement), << (left shift operator), >> (Right shift operator).

Below is an example program using bitwise operator:

#include <iostream>
#include <string>
using namespace std;
 
int main()
{
  int x=8,y=4,z;
  z = x^y;
  cout<<"Result of ^ : "<<z<<endl;
  z = x&y;
  cout<<"Result of & : "<<z<<endl;
  z = x|y;
  cout<<"Result of | : "<<z<<endl; 
  z = x<<4;
  cout<<"Result of << by 4 bits : "<<z<<endl;
  z = y>>4;
  cout<<"Result of >> by 4 bits : "<<z<<endl;
  z = ~3;
  cout<<"Result of ~ : "<<z<<endl;
}

KjboVvnW sXpMmSr34s2bGuuAEAtfPYio9GDW3QnPovQ7C30cPFkg9entqN2C0yiCOFjfdTNX9GRRl M9iSebwbGys 6GWhyIsT7R2PLemVRf4RXbm DuXeiOotH9lm15vlkXw

Credit Goes To Shiva Patra

Short-Hand Assignment Operator In C++:

These operators assign values to the variables. The assignment operators used in C++ are: –

  • = It assigns the value of the right operand to the left operand.
  • += It assigns a sum of two operands to left operands.
  • -= It assigns a difference of two operands to the left operand.
  • *= It assigns a product of two operands to the left operand. /= It assigns the quotient of two operands to the left operand.

An example on how to use assignment operators in a program is given below:

#include <iostream>
using namespace std;
 
int main (){
  float i = 5;

  cout<<"i = "<<i<<"\n";
  i += 10;
  cout<<"i += 10; makes i = "<<i<<"\n";
  i -= 6;
  cout<<"i -= 6; makes i = "<<i<<"\n";
  i *= 2;
  cout<<"i *= 2; makes i = "<<i<<"\n";
  i /= 3;
  cout<<"i /= 3; makes i = "<<i<<"\n";
}

J8nXwvk4R9Aj IkfU0ugPjIC9jMg1OWURo63CNgS mR34CQbTSe1la J gmNePN4kwrGoxtlkY BrVHn8M5yxdlnS5IcEhat6dqL

Miscellaneous Operators In C++:

These are the operators which are not specifically divided into any previous categories we discussed.

  • Condition? x: y => It works as an if-else statement, it returns x value if the condition is true else returns y.
  • & => It returns the address of a variable.
  • * =>It is a pointer, pointing to a variable.
  • Cast => It converts one data type to another.
  • sizeof() => It returns the size of the variable.
  • The comma(,) => It causes a sequence of operations to be performed.

So, the operators have their specific precedence value which means while using them in our program we have to remember and place them according to the table given below:

Vu1BoNiFrxDsYRGuPqviCq64b3NdwNiZmoDfqp8b8AKQgsz 5oYRr3w

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Default image
Shiva Patra
Hello, this is G. Shiva. Patra, your content writer for this website, is currently pursuing BCA for a graduation degree. I am here to write content based on C++ and similar kind of stuff. I like the tech world as it grows really fast and you have great things to learn every day which keep you away from boredom. So, I have some knowledge on very different topics related to tech like the dark web, deep web, onion rooting, IoT, Cybersecurity, and I always keep track of new devices and software used by them as every tech enthusiast does.
Articles: 9

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply