In this tutorial, we will learn about different Assignment Operators available in C++ programming language and go through each of these Assignment Operations in detail, with the help of examples.
C++ Assignment Operators
C++ Assignment Operators are used to optionally perform an action with given operands and assign the result back to given variable (left operand).
The syntax of any Assignment Operator with operands is
operand1 operator_symbol operand2
Operator Symbol – Example – Description
The following table specifies symbol, example, and description for each of the Assignment Operator in C++.
Operator Symbol | Arithmetic Operation | Example | Description |
---|---|---|---|
= | Simple Assignment | x = 2 | Assign x with 2. |
+= | Addition Assignment | x += 3 | Add 3 to the value of x and assign the result to x. |
-= | Subtraction Assignment | x -= 3 | Subtract 3 from x and assign the result to x. |
*= | Multiplication Assignment | x *= 3 | Multiply x with 3 and assign the result to x. |
/= | Division Assignment | x /= 3 | Divide x with 3 and assign the quotient to x. |
%= | Remainder Assignment | x %= 3 | Divide x with 3 and assign the remainder to x. |
&= | Bitwise AND Assignment | x &= 3 | Perform x & 3 and assign the result to x. |
|= | Bitwise OR Assignment | x |= 3 | Perform x | 3 and assign the result to x. |
^= | Bitwise-exclusive-OR Assignment | x ^= 3 | Perform x ^ 3 and assign the result to x. |
<<= | Left-shift Assignment | x <<= 3 | Left-shift the value of x by 3 places and assign the result to x. |
>>= | Right-shift Assignment | x >>= 3 | Right-shift the value of x by 3 places and assign the result to x. |
1. Simple Assignment
In the following example, we assign a value of 2 to x using Simple Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
//simple assignment
int x = 2;
cout << "x : " << x << endl;
}
Output
x : 2
Program ended with exit code: 0
2. Addition Assignment
In the following example, we add 3 to x and assign the result to x using Addition Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 2;
//addition assignment
x += 3;
cout << "x : " << x << endl;
}
Output
x : 5
Program ended with exit code: 0
3. Subtraction Assignment
In the following example, we subtract 3 from x and assign the result to x using Subtraction Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 2;
//subtraction assignment
x -= 3;
cout << "x : " << x << endl;
}
Output
x : -1
Program ended with exit code: 0
4. Multiplication Assignment
In the following example, we multiply 3 to x and assign the result to x using Multiplication Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 2;
//multiplication assignment
x *= 3;
cout << "x : " << x << endl;
}
Output
x : 6
Program ended with exit code: 0
5. Division Assignment
In the following example, we divide x by 3 and assign the quotient to x using Division Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 8;
//division assignment
x /= 3;
cout << "x : " << x << endl;
}
Output
x : 2
Program ended with exit code: 0
6. Remainder Assignment
In the following example, we divide x by 3 and assign the remainder to x using Remainder Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 8;
//remainder assignment
x %= 3;
cout << "x : " << x << endl;
}
Output
x : 2
Program ended with exit code: 0
7. Bitwise AND Assignment
In the following example, we do bitwise AND operation between x and 3 and assign the result to x using Bitwise AND Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 10;
//bitwise AND assignment
x &= 3;
cout << "x : " << x << endl;
}
Output
x : 2
Program ended with exit code: 0
8. Bitwise OR Assignment
In the following example, we do bitwise OR operation between x and 3 and assign the result to x using Bitwise OR Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 10;
//bitwise OR assignment
x |= 3;
cout << "x : " << x << endl;
}
Output
x : 11
Program ended with exit code: 0
9. Bitwise XOR Assignment
In the following example, we do bitwise XOR operation between x and 3 and assign the result to x using Bitwise XOR Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 10;
//bitwise OR assignment
x ^= 3;
cout << "x : " << x << endl;
}
Output
x : 9
Program ended with exit code: 0
10. Left-shift Assignment
In the following example, we left-shift x by 3 places and assign the result to x using Left-shift Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 10;
//left-shift assignment
x <<= 3;
cout << "x : " << x << endl;
}
Output
x : 80
Program ended with exit code: 0
11. Right-shift Assignment
In the following example, we right-shift x by 3 places and assign the result to x using Right-shift Assignment Operator.
main.cpp
#include <iostream>
using namespace std;
int main() {
int x = 10;
//right-shift assignment
x >>= 3;
cout << "x : " << x << endl;
}
Output
x : 1
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned what Assignment Operators are, and how to use them in C++ programs, with the help of examples.