0 votes
in C Plus Plus by
Comment on Assignment Operator in C++.

1 Answer

0 votes
by
Assignment operator in C++ is used to assign a value to another variable.

a = 5;

This line of code assigns the integer value 5 to variable a.

The part at the left of the =operator is known as an lvalue (left value) and the right as rvalue (right value). Lvalue must always be a variable whereas the right side can be a constant, a variable, the result of an operation or any combination of them.

The assignment operation always takes place from the right to left and never at the inverse.

One property which C++ has over the other programming languages is that the assignment operator can be used as the rvalue (or part of an rvalue) for another assignment.

Example:

a = 2 + (b = 5);

is equivalent to:

b = 5;

a = 2 + b;

Which means, first assign 5 to variable b and then assign to a, the value 2 plus the result of the previous expression of b(that is 5), leaves a with a final value of 7.

Thus, the following expression is also valid in C++:

a = b = c = 5;

assign 5 to variables a, b and c.

Related questions

+1 vote
asked Jan 20, 2021 in C Plus Plus by SakshiSharma
+1 vote
asked Mar 17, 2020 in C Plus Plus by SakshiSharma
...