Assignment operators are used to assign a value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. For example, the expression
C+=2
simply means that "C"
is the variable, the "+="
is the assignment operator, and lastly the "2"
is the value assigned to the variable.
Assignment operators supported by C++ language are shown in the table below.
Operator | Description | Example |
---|---|---|
= |
Simple assignment operator. It assigns values from right operands to the left side operand | C=A+B will assign value of A+B into C |
+= |
Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand | C+=A is equivalent to C=C+A |
-= |
Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. | C-=A is equivalent to C=C-A |
*+ |
Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C*=A is equivalent to C=C*A |
/= |
Divides AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. | C/=A is equivalent to C=C/A |
%= |
Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. | C%=A is equivalent to C=C%A |
<<= |
Left shift AND assignment operator. | C<<=2 is same as C=C<<2 |
>>= |
Right shift AND assignment operator | C=C>>2 is same as C=C>>2 |
&= |
Bitwise AND assignment operator | C&=2 is same as C=C&2 |
^= |
Bitwise exclusive OR AND assignment operator | C^=2 is same as C=C^2 |
|= |
Bitwise inclusive OR AND assignment operator | C |
Let’s write a code to include all the assignment operators.
#include <iostream>using namespace std;int main() {int a=21;int c;// using the simple assignment operatorc=a;cout << "Line 1: -= operator, value of c= "<<c<<endl;// using the add and assignment operatorc+=a;cout << "Line 2: += operator, value of c= "<<c<<endl;// using the subtract and assignment operatorc-=a;cout << "Line 3: -= operator, value of c= "<<c<<endl;// using the multiply and assignment operatorc*=a;cout << "Line 4: *= operator, value of c= "<<c<<endl;// using the divide and assignment operatorc/=a;cout << "Line 5: /= operator, value of c= "<<c<<endl;// lets give c another valuec=200;// using the modulo and assignment operatorc%=a;cout << "Line 6: %= operator, value of c= "<<c<<endl;// using the left shift and assignment operatorc<<=2;cout << "Line 7: <<= operator, value of c= "<<c<<endl;// using the right shift and assignment operatorc>>=2;cout << "Line 8: >>= operator, value of c= "<<c<<endl;// // using the bitwise and assignment operatorc&=2;cout << "Line 9: &= operator, value of c= "<<c<<endl;// using the bitwise exclusive or and assignment operatorc^=2;cout << "Line 10: ^= operator, value of c= "<<c<<endl;// using the bitwise inclusive or and assignment operatorc|=2;cout << "Line 11: |= operator, value of c=: "<<c<<endl;return 0;}
c += a
where c=21
and c=a
.c = c + a
, which arithmetically is c = 21 + 21
, hence the answer is 42
.