C++ Operators, Types and Examples
C++ Operators, Types and Examples
Operators are symbols which act on variables or other entities that are called operands
and perform mathematical or logical operations to modify their values and produce
results accordingly.
https://www.softwaretestinghelp.com/cpp‐operators/ 1/22
11/19/2020 C++ Operators, Types And Examples
FEATURED VIDEOS
Operators In C++
https://www.softwaretestinghelp.com/cpp‐operators/ 2/22
11/19/2020 C++ Operators, Types And Examples
Operators form the basic foundation of any programming language. Without operators,
we cannot modify or manipulate the entities of programming languages and thereby
cannot produce the desired results. C++ is very rich in built-in operators which we will
discuss in detail in this tutorial.
In C++ most of the operators are binary operators i.e. these operators require two
operands to perform an operation. Few operators like ++ (increment) operator are the
unary operator which means they operate on one operand only.
There is also a ternary operator in C++ called Conditional Operator which takes three
operands. We will learn about this in detail in the later part of the tutorial.
Arithmetic Operators
https://www.softwaretestinghelp.com/cpp‐operators/ 3/22
11/19/2020 C++ Operators, Types And Examples
The below Example demonstrates the first five arithmetic operators in C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
int op1=3,op2=4;
float op3=10.1,op4=5.4;
cout<<"Operands are op1 = "<<op1<<" op2 = "<<op2;
cout<<" op3 = "<<op3<<" op4 = "<<op4;
cout<<endl;
cout<<"op1 + op2 = "<<op1+op2<<endl;
cout<<"op1 ‐ op2 = "<<op1‐op2<<endl;
cout<<"op3 * op4 = "<<op3*op4<<endl;
cout<<"op3 / op4 = "<<op3/op4<<endl;
cout<<"op2 % op1 = "<<op2%op1<<endl;
}
Output:
The program defines operands first and then performs arithmetic operations on these
operands. This program demonstrates the usage of arithmetic operators in C++.
The next arithmetic operators that we are going to discuss are ++ and –. These are
called increment and decrement operators respectively. The increment operator
increases the value of the operand by 1 while the decrement operator decreases the
value of the operand by 1.
https://www.softwaretestinghelp.com/cpp‐operators/ 4/22
11/19/2020 C++ Operators, Types And Examples
x+=1;
x = x+1;
x -=1;
x = x-1;
The increment and decrement operators can be placed as a prefix as well as a suffix to
the operand. Depending on its placement, these operators have a different meaning to
the evaluation of an expression.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x=4,y;
y = ++x;
cout<<"PreIncrement:Value of x = "<<x;
cout<<endl;
cout<<"PreIncrement:Value of y = "<<y;
cout<<endl;
y = x‐‐;
cout<<"PostDecrement:Value of x = "<<x;
cout<<endl;
cout<<"PostDecrement:Value of y = "<<y;
cout<<endl;
}
https://www.softwaretestinghelp.com/cpp‐operators/ 5/22
11/19/2020 C++ Operators, Types And Examples
Output:
PreIncrement:Value of x = 5
PreIncrement:Value of y = 5
PostDecrement:Value of x =4
PostDecrement:Value of y =5
This is the program to demonstrate the increment and decrement operators. Note that
we have used pre-increment and post-decrement operators in this program. For the
first expression, y=++x, as this is pre-increment, x will be incremented first and then
the resultant value will be assigned to y. This is evident from the output that we have
obtained.
In the second expression y=x–, the value of x which is now 5, will be assigned to y first
and then the value of x will be decremented. Hence in the output, we can see that for
post-decrement operation, the value of y is 5 while x is 4.
Logical Operators
Operator Description
&& Logical AND: returns true if both conditions are true otherwise returns
false.
|| Logical OR: returns true if one of the conditions is true. Returns false
when both conditions are false.
C++ employs a short circuit method to evaluate logical expressions. In this, C++ has to
evaluate only the first expression/operand of the logical expression to provide the
result. For Example, for logical AND (&&) operator, C++ evaluates only the first
expression. If it’s false then the result will be false even if the second condition is true.
Similarly, for logical OR (||), it evaluates only the first expression. If the first expression
is true, then the result will be true so it need not evaluate the second expression.
https://www.softwaretestinghelp.com/cpp‐operators/ 6/22
11/19/2020 C++ Operators, Types And Examples
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a=10, b=8,c=12,d=14;
if(!(a==0))
cout<<"a is not zero"<<endl;
else
cout<<"a is zero"<<endl; if((a>b)&&(c<d))
cout<<"Logical AND is true"<<endl;
else
cout<<"Logical AND is false"<<endl;
if((a<c)||(b<d))
cout<<"Logical OR is true"<<endl;
else
cout<<"Logical OR is false"<<endl;
}
Output:
a is not zero
Logical AND is true
Logical OR is true
In the above program, we have made use of all the three logical operators in order to
evaluate expressions and print the results.
Relational Operators
Relational or comparison operators are used to compare two operands. The result of
the evaluation is either true or false.
Operator Description
!ERROR! unexpected Evaluates whether two operands are equal. Returns true if
operator '=' equal else returns false.
!=(not equal to) Complements ‘equal to’ operator. Returns true if operands are
not equal. False otherwise.
https://www.softwaretestinghelp.com/cpp‐operators/ 7/22
11/19/2020 C++ Operators, Types And Examples
<(less than) Returns true if the first operand is less than second. False
otherwise.
<=(less than equal Returns true if the first operand is less than or equal to the
to) second operand. False otherwise.
>(greater than) Returns true if the first operand is greater than second. False
otherwise.
>=(greater than Returns true if the first operand is greater than equal to the
equal to) second. False otherwise.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a=10, b=8,c=12,d=14;
if(a==b)
cout<<"a is equal to b"<<endl;
else
cout<<"a is not equal to b"<<endl;
if(c!=d)
cout<<"c is not equal to d"<<endl;
else
cout<<"c is equal to d"<<endl;
if((a+b) <= (c+d))
cout<<" (a+b) less than/equal to (c+d)"<<endl; if((a‐b)>=(d‐c))
cout<<"(a‐b) greater than/equal to (d‐c)"<<endl;
}
Output:
a is not equal to b
c is not equal to d
(a+b) less than/equal to (c+d)
(a-b) greater than/equal to (d-c)
In the above program, we see the usage of relational operators and the way in which
they evaluate the expressions provided.
Note that we can provide not only values but also variables and expressions in the
https://www.softwaretestinghelp.com/cpp‐operators/ 8/22
11/19/2020 C++ Operators, Types And Examples
conditional statements.
Bitwise Operators
Bitwise operators in C++ operate on bits of the operands provided. Bitwise operators
are applied only to integral types like integer, character, etc., and not on data types like
float, double, etc.
Operators Description
&( Binary AND) Performs AND operation on bits of operand 1 and operand 2.
<<( Binary left Shifts bits of the first operand to the left to a number of bits
shift operator) specified by the second operand.
>>( Binary right Shifts bits of the first operand to the right to a number of
shift operator) places specified by the second operand.
These bitwise operators operate on operands in a bit-by-bit manner. The truth tables
for AND, OR and XOR operations are given below.
Consider a and b as two bits on which AND, OR and XOR operations are to be carried
out.
0 0 0 0 0
1 0 0 1 1
0 1 0 1 1
https://www.softwaretestinghelp.com/cpp‐operators/ 9/22
11/19/2020 C++ Operators, Types And Examples
1 1 1 1 0
a=8 1000
a=4 0100
a&b 0000 = 0
a|b 1100 = 12
a^b 1100 = 12
In the above example, we see that the bitwise AND of 8 and 4 is 0. Bitwise OR of 8 and
4 is 12 and bitwise XOR of 8 and 4 is as well 12.
This is the way in which bitwise operations are performed by the bitwise operators.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a=8,b=4,c;
c = a&b;
cout<<"Result of & : "<<c<<endl;
c = a|b;
cout<<"Result of | : "<<c<<endl;
c = a^b;
cout<<"Result of ^ : "<<c<<endl;
c = a<<2;
cout<<"Result of << by 2 bits : "<<c<<endl; c = b>>2;
cout<<"Result of >> by 2 bits : "<<c<<endl;
c = ~3;
cout<<"Result of ~ : "<<c<<endl;
}
https://www.softwaretestinghelp.com/cpp‐operators/ 10/22
11/19/2020 C++ Operators, Types And Examples
Output:
Result of &:0
Result of | : 12
Result of ^ : 12
Result of << by 2 bits: 32
Result of >> by 2 bits: 1
Result of ~ : -4
In the above program, we demonstrated the usage of bitwise operators and also
printed the output of each of the operation.
Assignment Operators
Assignment operator “=” is used to assigning a value to a variable. The LHS of the
assignment operator is a variable and RHS is the value that is to be assigned to the
variable. The value on the right side must be of the same type as that of the variable
on the left-hand side.
Note the difference between ‘=’ and ‘==’ operators. The former is the assignment
operator and the later is the equality operator.
Assignment operation takes place from right to left. Apart from the assignment
operator ‘=’, there are other variations of assignment operator which are known as
‘compound assignment operators”. These operators perform an operation in addition to
the assignment.
Operator Description
+= Adds RHS operand to LHS operand and assigns the result in LHS operand.
-= Subtracts RHS operand to LHS operand and assigns the result to LHS
operand
*= multiplies RHS operand to LHS operand and assigns the result to LHS
operand
/= divides RHS operand to LHS operand and assigns the result to LHS
https://www.softwaretestinghelp.com/cpp‐operators/ 11/22
11/19/2020 C++ Operators, Types And Examples
operand
As shown in the above table, If x and y are operands, x+=y is equivalent to x = x+y.
Similarly,
x *= y is equivalent to x = x*y.
x /= y is equivalent to x = x/y.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x,y;
cout<<"Enter input variable y: "; cin>>y;
x = y;
cout<<"\nValue of x = "<<x<<endl;
int a = 3,b = 5,c = 8;
a += b;
c ‐= b;
cout<<"\na += b: "<<a;
cout<<"\nc ‐= b: "<<c;
a *= b;
b /= c;
cout<<"\na *= b: "<<a;
cout<<"\nb /= c: "<<b;
return 0;
}
Output:
Value of x = 4
a += b: 8
c -= b: 3
a *= b: 40
https://www.softwaretestinghelp.com/cpp‐operators/ 12/22
11/19/2020 C++ Operators, Types And Examples
b /= c: 1
Note: We can also combine the other binary operators like %, <<, >>, &, |, ^, etc. into
compound assignment statements in addition to the ones that are already
demonstrated.
Other Operators
So far we explored all the major operators in C++. There are some more additional C++
operators that need our attention.
sizeof is a unary operator that is used extensively in C and C++. Sizeof returns the size
of its operand. The return value is usually an unsigned integral type denoted by ‘size_t’.
Sizeof operator has many uses in C and C++ languages. It can be used to find out the
size of the variables, arrays or expressions and even to allocate the blocks of memory.
The conditional operator in C++ can be used as a replacement for if-else statement.
Condition? expression1:expression2;
https://www.softwaretestinghelp.com/cpp‐operators/ 13/22
11/19/2020 C++ Operators, Types And Examples
If the condition is true, expression 1 will be evaluated. If the condition is false, then
expression2 will be evaluated.
Please note that expression1 and expression2 need to be of the same data types in
order to avoid potential errors.
Comma operator that is represented as a token ‘,’ can be used as an operator as well
as a separator.
x = (y=4, y+1);
In this expression, we have two expressions on the right-side separated with a comma.
Here comma acts as an operator. First, the expression, y=4 will be evaluated. Then the
next expression y+1 will be evaluated by using the result of the first expression i.e. y=4.
Thus the value of y+1 will be 5 and this value will be assigned to x.
There are two operators that are used to access the individual members of classes,
structures or unions in C++. These are the dot operator (.) and arrow (->) operator. We
will learn these operators in detail when we learn object-oriented programming in C++.
The below Example demonstrates the usage of sizeof, Comma and Conditional
Operator.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x,y;
https://www.softwaretestinghelp.com/cpp‐operators/ 14/22
11/19/2020 C++ Operators, Types And Examples
x = (y=3,y+4);
cout<<"Value of x = "<<x;
y = (x<5)?0:1;
if(y == 0)
cout<<"\nVariable x is less than 5"<<endl;
else
cout<<"\nVariable x is greater than 5"<<endl;
cout<<"sizeof(x): "<<sizeof(x)<<"\t"<<"sizeof(y): "<<sizeof(y);
return 0;
}
Home Resources FREE EBooks QA Testing Courses
Output:
Automation Types Of Testing Tutorials
Value of x = 7
Variable x is greater than 5
sizeof(x): 4 sizeof(y): 4
As shown in the above program, first we have two variables declared and separated by
a comma. (comma as a separator). Next, we have a comma operator with two
expressions. As we can see from the output, the rightmost expression’s value is
assigned to variable x. Next, we demonstrate the conditional operator to evaluate if x is
less than 5.
Finally, we demonstrate the usage of the sizeof operator. Here we use the sizeof
operator to get the size of the variables x and y. As both are integer variables, the size
returned is 4 bytes.
We have already seen almost all the C++ operators and we know that they can be used
https://www.softwaretestinghelp.com/cpp‐operators/ 15/22
11/19/2020 C++ Operators, Types And Examples
in expressions to carry out specific operations. But the expressions we have seen in
What is
examples are simple System
and Testing
straightforward. However, depending on our requirements,
expressions tend to become more and more complex.
Such complex expressions will have more than one operator and many operands. In
such a situation, we need to evaluate which operator is to be evaluated first.
x = 4 + 5 / 3;
Here we have + and / operators and we need to decide which expression will be
evaluated first. In mathematical terms, we know that division will be carried out before
addition. Thus the expression will become x = 4 + (5/3) = 5.
But when the compiler is faced with such a situation, we also need to have a similar
mechanism to decide the order of operations, so that it can properly evaluate the
expression.
This order in which the operators in a compound expression are evaluated is called the
“Precedence” of the operator. C++ has defined precedence for all the operators and the
operators with higher precedence are evaluated first.
What happens when we have two operators side by side in an expression with the
same precedence? This is where the associativity of an operator comes into the
picture.
https://www.softwaretestinghelp.com/cpp‐operators/ 16/22
11/19/2020 C++ Operators, Types And Examples
2 L->R () Parentheses
() Function call
() Initialization
{} Uniform initialization (C++11)
type() Functional cast
type{} Functional cast (C++11)
[] Array subscript
. Member access from the object
-> Member access from object ptr
++ Post-increment
–– Post-decrement
typeid Run-time type information
const_cast Cast away const
dynamic_cast Run-time type-checked cast
reinterpret_cast Cast one type to anotherCompile-time
static_cast type-checked cast
5 L->R * Multiplication
/ Division
% Modulus
6 L->R + Addition
- Subtraction
https://www.softwaretestinghelp.com/cpp‐operators/ 17/22
11/19/2020 C++ Operators, Types And Examples
12 L->R | Bitwise OR
14 L->R || Logical OR
Notes:
Precedence level 1 is the highest precedence level, and level 17 is the lowest.
Operators with a higher precedence level get evaluated first.
L->R means left to right associativity.
https://www.softwaretestinghelp.com/cpp‐operators/ 18/22
11/19/2020 C++ Operators, Types And Examples
Conclusion
This is all about the operators in C++.
We have discussed almost all the operators. Some specific operators that are present
in the above precedence table which we have not discussed, will be discussed
according to the topics that we cover in our upcoming tutorials.
Recommended Reading
Unix Shell Script Arithmetic and Boolean Operators Examples
Python Operators
New/Delete Operators In C++ With Examples
Python Data Types
Unix Conditional Statements: If Then Else and Relational Operators
Python DateTime Tutorial with Examples
HTML Injection Tutorial: Types & Prevention with Examples
Cut Command in Unix with Examples
About SoftwareTestingHelp
Helping our community since 2006! Most popular portal for Software
professionals with 100 million+ visits and 300,000+ followers! You will
absolutely love our tutorials on QA Testing, Development, Software Tools and
Services Reviews and more!
https://www.softwaretestinghelp.com/cpp‐operators/ 19/22
11/19/2020 C++ Operators, Types And Examples
Recommended Reading
https://www.softwaretestinghelp.com/cpp‐operators/ 20/22
11/19/2020 C++ Operators, Types And Examples
https://www.softwaretestinghelp.com/cpp‐operators/ 21/22
11/19/2020 C++ Operators, Types And Examples
https://www.softwaretestinghelp.com/cpp‐operators/ 22/22