Assignment operators in C++

In this tutorial, we are going to discuss the Assignment operators in C++. These assignment operators are used for assignment purposes, which means when we are required to assign values to the variables.

We will see what are some of the different available symbols for assignment operators, and we will try to use them in some examples, so that it becomes easy for us to understand the concept, and later we can use them comfortably in our C++ programs.

Assignment operators in C++

OperatorDescriptionExample
=Assignment operatora = 5;
Assign the value 5 to variable a.
+=Add and assigna += 5;
Add 5 to the existing value in variable a and store the result back to a.
-=Subtract and assigna -= 2;
Subtract 2 from the existing value in variable a and store the result back to a.
*=Multiply and assigna *= 4;
Multiply the existing value in variable a by 4 and store the result back to a.
/=Divide and assigna /= 2;
Divide the existing value in variable a by 2 and store the result back to a.
%=Modulus and assigna %= 2;
Divide the existing value in variable a by 2 and store the remainder back to a.
&=Bitwise AND and assigna &= 4;
Perform a bitwise AND operation between the value in variables a and 4, and store the result back to a.
|=Bitwise OR and assigna |= 4;
Perform a bitwise OR operation between the value in variables a and 4, and store the result back to a.
^=Bitwise XOR and assigna ^= 4;
Perform a bitwise XOR operation between the value in variables a and 4, and store the result back to a.
<<=Left shift and assigna <<= 4;
Perform a bitwise left shift operation on the value in variable a by 4 positions, and store the result back to a.
>>=Right shift and assigna >>= 4;
Perform a bitwise right shift operation on the value in variable a by 4 positions, and store the result back to a.

As you can see in the above table, we have listed some assignment operator symbols. Let’s have a look at some of the symbols in action through a C++ program, and for the rest, you can use them as per the requirement in the C++ programs.

#include <iostream>
int main()
{
int a = 10, b = 2;
a +=b; // similar to a = a+b;
a -=5; // similar to a = a – 5;
std::cout << “Value of a: “<<a << std::endl;
// you can try others.
return 0;
}

As you can see in the above program, we have tried using some assignment operator symbols. For the rest of the symbols, you can use them as and when required in your C++ programs.

Output –

Value of a: 7

As you can see, we have got the output as 7, since first, we are adding 2 to 10, making it 12, and then subtracting 5 from the result, which makes it 7. So, you can use the assignment operators as and when required in your C++ program.

If you find this tutorial to be interesting, you can explore our other tutorials related to C++ programming language to learn more about C++ programming language. You can also explore our other courses, related to Python, Machine learning, and Data Science, through which, you can upskill yourself.

FAQs related to Assignment operators in C++

Q: What are assignment operators?

Ans: Assignment operators are used for assigning some value to some variable. We have different assignment operator symbols to use in our C++ programs.

Q: What are the different symbols for assignment operators in C++?

Ans: Different symbols for operators include (=, +=, -=, *=, /=, %=, etc)