C++ operators and how to use them? Operators in C++ programming
Table of Contents
Description:
C++ operators In this article, you will in detail, what are operators and how to use them in C++ programming. I will explain everything with the help of C++ programs.
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Arithmetic operators:
The arithmetic operators are the symbols that represent arithmetic operations. These are used in arithmetic expressions. Each arithmetic operator operates upon two numeric values( constants and variables ) and returns a value.
The following arithmetic operators are used in C++:
Operators
Meaning
+
Addition
_
Subtraction
*
Multiplication
/
Division
%
For reminder
The ‘ + ’ and ‘ – ‘ symbols are quite familiar to you. The ‘ + ’ symbol indicates addition. Whereas ‘ – ’ symbol indicates subtraction.
The ‘ * ’ (asterisk ) is used in c/ C++ to indicate multiplication.
The ‘ / ’ (slash) is used to indicate for both real and integer division. If both operands in division operators are integers the result will be integer. For example:
5/3 > 1
3/5 > 0
Whereas on the other hand real division is performed producing the real result if both or either of the operand is real for example:
5.0 / 2.0 > 2.5
5 / 3.0 > 1.666667
3.0 / 4 > 0.75
While using the division ( / ) operator, the second operand must be a non zero.
The percent sign ( % ) produces a modulus or a reminder of an integer division. % computes the arithmetic remainder after a first integer value is divided by a second integer value. For the modulus operator, both operands must be integers and the second operand must be non zero otherwise it will not work.2 % 2 0
3 % 2 > 1
11 % 3 > 2
2 % 10 >2
5 % 10 > 5
All arithmetic operators except the remainder operator are used for all types of numeric data.
Example how to use arithmetic operators in C++ programming:
#include <iostream> using namespace std; int main() { int a,b,c,d,e; a= 1+2; b= 105; c= 6*2; d= 4/2; e= 5%2; cout<<"addition of 1 and 2 is :"<<a<<endl; cout<<"subtraction of 10 and 5 is :"<<b<<endl; cout<<"multiplication of 6 and 2 is :"<<c<<endl; cout<<"division of 4 and 2 is :"<<d<<endl; cout<<"remainder of 5 and 2 is :"<<e<<endl; return 0; } Output: addition of 1 and 2 is :3 subtraction of 10 and 5 is :5 multiplication of 6 and 2 is :12 division of 4 and 2 is :2 remainder of 5 and 2 is :1
Increment ( ++ ) and Decrement ( — ) operators:
Unlike the other programming languages, C / C++ also supports the ++ “Increment” and — “Decrement” operators to increment and decrement variables by one respectively. Let’s take the example of counting the number of persons entering a room or leaving the room. You can also use it for counting the items etc. The increment operator ( ++ ) is used to add 1 to variable and decrement operator ( — ) is used to subtract 1 from variable. These operators can be used on either side of the variable. So let’s discuss these operators in detail.
Increment ( ++ ) operator:
The increment operator is represented by a pair of plus (++) sign. It is utilized to add 1 to the value of an integer variable. This operator can be utilized previously or after the variable name.
For instance:
In the event that we need to add 1 to a value of variable ab it is regularly composed or written as
ab = ab+1;
by utilizing the increment operator ” ++ ” it is normally written as
ab++;
The increment operator can be written either before or after the variable name. On the off chance that it is composed before the variable, it is known as prefixing. In the event that it is composed after the variable it is known as postfixing. Prefix and postfix operators have a diverse impact when they are utilized in expression.
Prefix increment operator:
At the point when an increment operator is utilized in prefix mode in an expression it adds 1 to the value of the variable before the value of the variable is utilized in the expression.
Example: how to use prefix increment operators in C++ programming:
#include <iostream> using namespace std; int main() { int a, b, c , sum; a=1; b=1; c=3; sum= a+b+c+(++c); cout<<"sum = "<<sum<<endl; cout<<"c = "<<c; return 0; } Output: sum = 9 c = 4
In the above program 1 will be added to the value of c before it is used in the expression thus after execution the sum will be equal to 9 and value of c will be 4.
Postfix increment operator:
When an increment operator is used in postfix mode in an expression it adds 1 to the value of the variable after the value of the variable has been used in the expression. For example if in the above example increment operator is used in postfix mode the result will be different the statement will be as shown below:
Sum= a + b + c + C++;
In this case, 1 will be added to the value f c after its existing value has been used in the expression. Thus after execution, the sum will be equal to 5 and the value of c will be 4
Decrement ( — ) operators:
The decrement operator is represented by double minus ( — ) sign it is used to subtract 1 from the value of an integer variable.
For example to subtract 1 from the value of a variable ab the decrement statement is written as
Ab–;
Or
ab;
Prefix decrement operator:
When a decrement operator is used in prefix mode in an expression it subtracts 1 from the value of the variable before the value of the variable is used in the expression.
Example how to use prefix decrement operators in C++ programming:
#include <iostream> using namespace std; int main() { int a, b, c , sum; a=1; b=1; c=3; sum= a+b+c+(c); cout<<"sum = "<<sum<<endl; cout<<"c = "<<c; return 0; } Output: sum = 7 c = 2
In the above program, 1 will be subtracted from the value of c before it is used in the expression thus after execution the sum will be equal to 7, and the value of c will be 2.
Postfix decrement operator:
When a decrement operator ( — ) is used in postfix mode in an expression then it subtracts 1 from the value of the variable, after the value of the variable has been used in the expression. For example if in the above example decrement operator is used in postfix mode the result will be different the statement will be as shown below:
Sum= a + b + c + c–;
In this case, 1 will be subtracted from the value f c after its existing value has been used in the expression. Thus after execution, the sum will be equal to 5 and the value of c will be 2.
Assignment operators ( = ):
The assignment operator ( = ) is used to assign values to variables. It is used to assign a numeric or character value to a variable. However the string data can also be assigned to a variable with this operator.
The assignment operator assesses an expression and afterward assigns its value to a variable. The assignment operator ( = ) is utilized to assign the calculated value to a variable. The expression is written on the right side of the assignment operator. The variable to which the value of the expression is to be assigned is written on the left side of the assignment operator.
The syntax of the assignment operator is
Var = expression;
Where:
Var:
Variable name of which the value of expression is assigned. The variable type must be the same as the type of value returned by the expression.
Expression:
An expression may be a combination of operands (variable and constants ) and arithmetic operators.
A character can also be assigned but a string cannot be assigned to a string variable.
Example how to use assignment operators in C++ programming:
#include <iostream> using namespace std; int main() { int year, month; year=20; month=year*12; cout << "year =" <<year<< endl; cout << "month =" <<month<< endl; return 0; } Output: year =20 month =240
Relational operators:
Relational operators are used to form conditions. These are used to write a conditional expression that describes the relationship between two values .these operations allow us to compare two values to see whether they are equal to each other unequal or whether one is greater than the other or vice versa. Relational operators return the 1 (true) or 0 (False). Don’t confuse the assignment operator ( = ) with the equals relational operator (==) the six relational operators in C++ are:
Greater than ( > ) :
It is used to test if one expression is greater than the other expression. For example, if expression1 and expression2 are two expressions then
expression1 > expression2
If expression1 greater than expression2 then this expression returns a true value. If expression1 is less than expression2 then this expression returns a false value.
Greater than or equal to ( >= ) :
It is used to test if one expression is greater than or equal to the other expression. For example if expression1 and expression2 are two expression then
expression1 >= expression2
If expression1 greater than or equal to expression2 then this expression returns a true value. If expression1 is less than expression2 then this expression returns a false value.
Less than ( < ) :
It is used to test if one expression is less than the other expression. For example, if expression1 and expression2 are two expressions then
expression1 < expression2
if expression1 is less than expression2 then this expression returns a true value. if expression1 is greater than expression2 then this expression returns a false value.
less than or equal to ( < =) :
It is used to test if one expression is less than or equal to the other expression. For example if expression1 and expression2 are two expression then
expression1 <= expression2
if expression1 less than or equal to expression2 then this expression returns true value. if expression1 is greater than expression2 then this expression returns a false value.
Equal to ( ==) :
It is used to test if one expression is equal to the other expression. For example if expression1 and expression2 are two expression then
expression1 == expression2
if expression1 equal to expression2 then this expression returns a true value. if expression1 is not equal to (less than or greater than ) expression2 then this expression returns a false value.
not Equal to ( !=) :
It is used to test if one expression is not equal to the other expression. For example if expression1 and expression2 are two expression then
expression1 != expression2
if expression1 is not equal to (less than or greater than) expression2 then this expression returns true value. if expression1 is equal to expression2 then this expression returns a false value.
Example: how to use Relational operators greater than ( > ) and less than or equal to (<=) in C++ programming:
#include <iostream> using namespace std; int main() { float current_units, previous_units, units,bill; cout<<"Enter the current reading : "; cin>>current_units; cout<<"enter the previous readig : "; cin>>previous_units; units= current_units  previous_units; if (units > 300) { bill= units*3.5 + units*0.05/100.0; cout<<"Electricity bill is = "<<bill; } if(units <= 300) { bill= units*3.5 + units*0.05/100.0; cout<<"Electricity bill is= "<<bill; } return 0; } Output: Enter the current reading : 42342 Enter the previous reading : 2323 Electricity bill is = 140087
Examplea: how to use Relational operators equal to (==) in C++ programming:
using namespace std; int main() { int n; cout<<"Enter a number : "; cin>>n; if(n%3==0) { cout<<"The number "<<n<<" is divisible by 3 "; } else { cout<<"The number "<<n<<" is not divisible by 3 "; } return 0; } Output: Enter a number : 4 The number 4 is not divisible by 3 Output: Enter a number : 9 The number 9 is divisible by 3
Example how to use Relational operators not equal to (!=) in C++ programming:
#include <iostream> using namespace std; int main() { int n; cout<<"Enter a number : "; cin>>n; if(n != 10) { cout<<"The number "<<n<<" is not equal to 10 "; } else { cout<<"The number "<<n<<" is eqaual to 10 "; } return 0; } Output: Enter a number : 5 The number 5 is not equal to 10 Output: Enter a number : 10 The number 10 is not equal to 10
Logical Operators:
More than one condition can be combined and tested in an if statement by connecting them with logical operator &&(AND), (OR), and ! (NOT). Such condition expression is referred to as Boolean expression complex or compound expression.
A compound expression with && logical expression is true of both of the relational expression are true otherwise it is false for examples:
If((marks > 70) && (marks < 80))
Cout<<”garde is A”;
Suppose if marks is 75 the (marks >= 70) && (marks < 80)) is true if marks has the value 60 the (marks >= 70) && (marks < 80)) is false.
The compound expression with () logical operation is true if any of the conditions is/are true and false only in the one case when all conditions are false. For example:
If((choice == ‘Y’)  (choice == ‘y’))
goto top;
Thus the logical (choice == ‘Y’)  (choice == ‘y’)) is true if either of the relational expressions is true otherwise it is false.
These definitions are summarized in the following truth table which displays app possible values for two decisionmaking conditions X and Y and the corresponding values of the logical expression.
X
 Y
 (X&&Y)
 (XY)

true  true  true  True 
true  False  False  True 
False  True  False  True 
False  False  False  False 
now we discuss each of them in detail. There are three type of logical operators in C++ which are:
 && à AND Operator
  à OR Operator
 ! à NOT Operator
The && (AND) Operator:
The && (AND ) operator is used to combine two or more relational expression. If all relational expressions are true then the output returned by the compound expression is true. If anyone relational expression in the compound expression is false the output is false.
Example how to use logical operators && (AND) in C++ programming:
#include <iostream> using namespace std; int main() { int a,b,c; cout<<"Enter 1st number : "; cin>>a; cout<<"Enter 2nd number : "; cin>>b; cout<<"Enter 3rd number : "; cin>>c; if ((a>b)&& (a>c)) cout<<"1st value is greater"; else if((b>a)&& (b>c)) cout<<"2nd value is greater"; else cout<<"3rd value is greater"; return 0; } Output: Enter 1st number : 4 Enter 2nd number : 2 Enter 3rd number : 1 1st value is greater
The  (OR) Operator:
The  (OR) operator is used to combine more than one relational expression. if any one of the given relational expression is true the output will be true otherwise the output will be false.
Example how to use logical operators  (OR) in C++ programming:
#include <iostream> using namespace std; int main() { int a; char op; cout<<"Enter 1st number : "; cin>>a; cout<<"Enter any character : "; cin>>op; if((a>0) (op=='y')(op=='n')) cout<<"condition is true"; else cout<<"relational expression false"; return 0; } Output: Enter a number : 8 Enter any character : y condition is true output: Enter a number : 2 Enter any character : k relational expression false
The ! (NOT) Operator:
The ! (NOT ) operator is also known as the unary operator. It inverts the value returned by the relational expression or the compound expression.
Example how to use logical operators ! (NOT) in C++ programming:
#include <iostream> using namespace std; int main() { int a,b; cout<<"Enter 1st number : "; cin>>a; cout<<"Enter 2nt number : "; cin>>b; if(!(a<b)){ cout<<"1st is greater "; } else { cout<<"2nd is greater"; } return 0; } Output: Enter 1st number : 3 Enter 2nd number : 1 1st is greater Output: Enter 1st number : 5 Enter 2nd number : 6 2nd is greater
Bitwise Operators
The bitwise operators are utilized to access the computer hardware. Utilization of bitwise operators requires information on various number systems and conversion numbers from one system into another number system. The data inside the computer is represented in binary form, for example in a succession of 0s and 1s. 8pieces are utilized to represent one character inside the computer. The bitwise operators are used to manipulate the data inside the computer. These are also used in networking software to move data from one computer to another in the network. The commonly used bitwise operators and their details are discussed in my previous tutorial the link is given below.
The Bitwise && (AND) Operator:
The bitwise AND operator is used to compare the contents of two operands (of int data type) on bitbybit basis. This is the type of the operator that I frequently use in the Arduino programs. It returns 1 if he corresponding bit in both the operands is l otherwise it returns 0.
The Bitwise  (OR) Operator:
The bitwise OR operator is used to compare the contents of two operands (of int data type) on bit by bit basis. It returns 1 of any one of the two bits is 1, otherwise it returns 0. It is represented by the vertical bar (  ).
The Bitwise ^ (XOR) Operator:
The bitwise XOR operator is used to compare the contents to two operands (of int data type) one bitbybit basis. It returns 1 if the bits in the corresponding operands are different otherwise it returns 1 (if corresponding bits are same). It is represented by the caret ( ^ ) sign.
The Bitwise ~ (complement) Operator:
The bitwise complement operator is used to operate on an expression. It inverts the bits of the expression. It takes each bit of the operand and converts it to 1 if it is a 0 an to 0 it if is 1. It is represented by the tilde ( ~ ) sign.
The LeftShift (<<) Operator
The left shift operator is used to shift a specified number of bits of an operand to the left. It has two operands. The first operand on the left handside of the operator is the constant or variable the operator is the constant or variable whose bits are to be shifted. The second operand on the righthandside of the operator specified the number of bits that are to be shifted to the left. The right shift operator is represented by the (<<) sign
The RightShift (>>) Operator :
The right shift operator is used to shift a specified number of bits of an operand to the right. It has two operands. The first operand at the left hand side of the operator is the constant or variable whose bits are to be shifted. The second operand at the right hand side specified the number of bits that are to be shifted to the right.when the specified number of the bits of the operand are shifted to the right then the same number of bits on the left are filled with 0s. The right shift operator is represented by the (>>) sign
The ‘sizeof’ Operator:
In C or C++ data are mainly divided into three different types. These are:
Character data type:
The characters (Punctuation character, digits, and letters) are represented by the char data type, the short data type is the same size as char usually one byte.
Integer data type:
Integer data like, “3, 56, 654, 7, etc” (whole numbers) are represented by int and long.
Real data type:
Numbers including decimal fractions “43.54, 344.3, etc” are represented by float double or long double.
Now to find the size of these data type in C or C++ we have an operator which is called sizeof operator.
So, if you are required to find the size of data or data type then you are supposed to use the sizeof operator. It has the following general format:
sizeof data
Or
sizeof (data type)
Example how to use sizeof operator in C++ programming:
#include <iostream> using namespace std; int main() { cout<<"size of integer is: "<<sizeof(int)<<endl; cout<<"size of float is: "<<sizeof(float)<<endl; cout<<"size of double is: "<<sizeof(double)<<endl; cout<<"size of character is: "<<sizeof(char); return 0; } Output: size of integer is: 4 size of float is: 4 size of double is: 8 size of character is: 1
Comma “,” C++ operator:
In the below program example comma operator is used, comma operator is also known as sequence point operator. It is use to put more than one expression. On a single line separated by commas as use in the below program.
Example how to use comma operator in C++ programming:
#include <iostream> using namespace std; int main() { int z= 5, result; result= z + 5,cout<<result; return 0; } Output: 10
Priority of operators:
If an expression has two or more operators then there is a certain order in which these operations are performed by the computer system. This order is referred to as the order of precedence. The precedence order is given below
 All parentheses are evaluated first.
 Multiplication division and modulus operators are performed first these operators have the same level of precedence they are performed in left to right as they are encountered in an arithmetic expression. Further if an expression contains several like operations evaluation also proceeds from left to right.
 Addition and subtraction will be performed next. They also have same level of precedence so performance is given to the operator which occurs first.
Parentheses are used to change the priority order because C/C++ programming language evaluates expressions in parentheses first, if parentheses are nested then evaluation begins with the innermost pairs and works outward.