Related Tags

c++
communitycreator

# What are assignment operators in C++? Onyejiaku Theophilus Chidalu

### Overview

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

### Using the assignment operators

Let’s write a code to include all the assignment operators.

### Code

#include <iostream>
using namespace std;

int main() {
int a=21;
int c;
// using the simple assignment operator
c=a;
cout << "Line 1:  -= operator, value of c= "<<c<<endl;
// using the add and assignment operator
c+=a;
cout << "Line 2:  += operator, value of c= "<<c<<endl;
// using the subtract and assignment operator
c-=a;
cout << "Line 3:  -= operator, value of c= "<<c<<endl;
// using the multiply and assignment operator
c*=a;
cout << "Line 4:  *= operator, value of c= "<<c<<endl;
// using the divide and assignment operator
c/=a;
cout << "Line 5:  /= operator, value of c= "<<c<<endl;
// lets give c another value
c=200;
// using the  modulo and assignment operator
c%=a;
cout << "Line 6:  %= operator, value of c= "<<c<<endl;
// using the left shift  and assignment operator
c<<=2;
cout << "Line 7:  <<= operator, value of c= "<<c<<endl;
// using the right shift and assignment operator
c>>=2;
cout << "Line 8:  >>= operator, value of c= "<<c<<endl;
// // using the bitwise and assignment operator
c&=2;
cout << "Line 9:  &= operator, value of c= "<<c<<endl;
// using the bitwise exclusive or and assignment operator
c^=2;
cout << "Line 10:  ^= operator, value of c= "<<c<<endl;
// using the bitwise inclusive or and assignment operator
c|=2;
cout << "Line 11:  |= operator, value of c=: "<<c<<endl;
return 0;
}

### Explanation

• In line 9 in the program above c += a where c=21 and c=a.
• The given expression is the same as c = c + a, which arithmetically is c = 21 + 21, hence the answer is 42.
• This same technique applies to every other expressionassignment operator present in the code.
• Taking a look at the table of operators with their descriptions and examples will make it easier to understand.

RELATED TAGS

c++
communitycreator

CONTRIBUTOR Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time 