Last Updated : 15 Nov, 2023
Improve
In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign some value to the variables in C++ or in other words, it is used to store some kind of information.
Syntax
variable = value;
The right-hand side value will be assigned to the variable on the left-hand side. The variable and the value should be of the same data type.
The value can be a literal or another variable of the same data type.
Example
C++
// C++ program to illustrate the use of assignment operator
#include <iostream>
using
namespace
std;
int
main()
{
// Declare an integer variable
int
x;
// Assign the value 20 to variable x using assignment
// operator
x = 20;
cout <<
"The value of x is: "
<< x << endl;
return
0;
}
Output
The value of x is: 20
Compound Assignment Operators
In C++, the assignment operator can be combined into a single operator with some other operators to perform a combination of two operations in one single statement. These operators are called Compound Assignment Operators. There are 10 compound assignment operators in C++:
- Addition Assignment Operator ( += )
- Subtraction Assignment Operator ( -= )
- Multiplication Assignment Operator ( *= )
- Division Assignment Operator ( /= )
- Modulus Assignment Operator ( %= )
- Bitwise AND Assignment Operator ( &= )
- Bitwise OR Assignment Operator ( |= )
- Bitwise XOR Assignment Operator ( ^= )
- Left Shift Assignment Operator ( <<= )
- Right Shift Assignment Operator ( >>= )
Lets see each of them in detail.
1. Addition Assignment Operator (+=)
In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way.
Syntax
variable += value;
This above expression is equivalent to the expression:
variable = variable + value;
Example
C++
// C++ program to illustrate the addition assignment operator
#include <iostream>
using
namespace
std;
int
main()
{
// Initialize a variable to the store the total
int
total = 0;
total += 10;
total += 20;
total += 30;
total += 40;
total += 50;
// Display the final total
cout <<
"The total is: "
<< total << endl;
return
0;
}
Output
The total is: 150
2. Subtraction Assignment Operator (-=)
The subtraction assignment operator (-=) in C++ enables you to update the value of the variable by subtracting another value from it. This operator is especially useful when you need to perform subtraction and store the result back in the same variable.
Syntax
variable -= value;
This above expression is equivalent to the expression:
variable = variable - value;
Example
C++
// C++ program to illustrate the substraction assignment
// operator
#include <iostream>
using
namespace
std;
int
main()
{
int
x = 20;
int
y = 5;
// Using the subtraction assignment operator
x -= y;
cout <<
"After subtraction x is now: "
<< x << endl;
return
0;
}
Output
After subtraction x is now: 15
3. Multiplication Assignment Operator (*=)
In C++, the multiplication assignment operator (*=) is used to update the value of the variable by multiplying it with another value.
Syntax
variable *= value;
This above expression is equivalent to the expression:
variable = variable * value;
Example
C++
#include <iostream>
using
namespace
std;
int
main() {
int
x = 7;
// Initialize x to 7
x *= 4;
cout <<
"The updated value of x is: "
<< x << endl;
return
0;
}
Output
The updated value of x is: 28
4. Division Assignment Operator (/=)
The division assignment operator divides the variable on the left by the value on the right and assigns the result to the variable on the left.
Syntax
variable /= value;
This above expression is equivalent to the expression:
variable = variable / value;
Example
C++
// C++ program to illustrate the use of divide assignment
// operator
#include <iostream>
using
namespace
std;
// driver code
int
main()
{
double
x = 30.0;
// dividing x by 4 and assigning the value to x
x /= 4.0;
cout <<
"value : "
<< x << endl;
return
0;
}
Output
value : 7.5
5. Modulus Assignment Operator (%=)
The modulus assignment operator calculates the remainder when the variable on the left is divided by the value or variable on the right and assigns the result to the variable on the left.
Syntax
variable %= value;
This above expression is equivalent to the expression:
variable = variable % value;
Example
C++
// C++ program to illustrate the use of modulus assignment
// operator
#include <iostream>
using
namespace
std;
int
main()
{
int
a = 15;
// finding the remainder when a is divided by 15 and
// assigning it to a again
a %= 5;
cout <<
"value : "
<< a << endl;
return
0;
}
Output
value : 0
6. Bitwise AND Assignment Operator (&=)
This operator performs a bitwise AND between the variable on the left and the value on the right and assigns the result to the variable on the left.
Syntax
variable &= value;
This above expression is equivalent to the expression:
variable = variable & value;
Example
C++
// C++ program to illustrate the use of Bitwise AND
// Assignment Operator
#include <iostream>
using
namespace
std;
int
main()
{
int
a = 9;
// using Bitwise AND Assignment Operator
a &= 3;
cout <<
"value : "
<< a << endl;
return
0;
}
Output
value : 1
7. Bitwise OR Assignment Operator (|=)
The bitwise OR assignment operator performs a bitwise OR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.
Syntax
variable |= value;
This above expression is equivalent to the expression:
variable = variable | value;
Example
C++
// C++ program to illustrate the use of Bitwise OR
// Assignment Operator
#include <iostream>
using
namespace
std;
int
main()
{
int
a = 5;
// 0101 in binary
// using Bitwise OR Assignment Operator
a |= 2;
cout <<
"value : "
<< a << endl;
return
0;
}
Output
value : 7
8. Bitwise XOR Assignment Operator (^=)
The bitwise XOR assignment operator performs a bitwise XOR between the variable on the left and the value or variable on the right and assigns the result to the variable on the left.
Syntax
variable ^= value;
This above expression is equivalent to the expression:
variable = variable ^ value;
Example
C++
// C++ program to illustrate the use of Bitwise XOR
// Assignment Operator
#include <iostream>
using
namespace
std;
int
main()
{
int
a = 7;
// using Bitwise XOR Assignment Operator
a ^= 3;
cout <<
"value : "
<< a << endl;
return
0;
}
Output
value : 4
9. Left Shift Assignment Operator (<<=)
The left shift assignment operator shifts the bits of the variable on the left to left by the number of positions specified on the right and assigns the result to the variable on the left.
Syntax
variable <<= value;
This above expression is equivalent to the expression:
variable = variable << value;
Example
C++
// C++ program to illustrate the use of Left Shift
// Assignment Operator
#include <iostream>
using
namespace
std;
int
main()
{
int
a = 9;
// using Left Shift Assignment Operator
a <<= 4;
cout <<
"value : "
<< a << endl;
return
0;
}
Output
value : 144
10. Right Shift Assignment Operator (>>=)
The right shift assignment operator shifts the bits of the variable on the left to the right by a number of positions specified on the right and assigns the result to the variable on the left.
Syntax
variable >>= value;
This above expression is equivalent to the expression:
variable = variable >> value;
Example
C++
// C++ program to illustrate the use of Right Shift
// Assignment Operator
#include <iostream>
using
namespace
std;
int
main()
{
int
a = 19;
// using Right Shift Assignment Operator
a >>= 4;
cout <<
"value : "
<< a << endl;
return
0;
}
Output
value : 1
Also, it is important to note that all of the above operators can be overloaded for custom operations with user-defined data types to perform the operations we want.
maha123
Improve
Next Article
Move Assignment Operator in C++ 11