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:
- Arithmetic operator:
- Binary operator
- Unary operator
- Relational operator
- Logical operator
- Bitwise Operator
- Assignment operators
- Miscellaneous operator
For a better understanding, look at the diagram below.

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; }
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; }
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; }
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; }
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; }
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; }
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"; }
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:
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.