In C++, an Assignment Operator is used to store a value in a memory location (variable). While the simple equals sign (=) is the most common, C++ provides a variety of Compound Assignment Operators that combine an arithmetic operation with an assignment.
Theoretically, the assignment operation is the foundation of "state" in a program—it allows variables to evolve as the program executes.
To understand assignment, you must understand the two components of an assignment statement:
L-value (Left value): This must be a memory location that can hold data (usually a variable). It represents a persistent "container."
R-value (Right value): This is the data being stored. It can be a literal constant, another variable, or the result of a complex expression.
The Rule: The R-value is always evaluated first, and its result is then copied into the L-value's memory address.
The basic assignment operator copies the value from the right side to the variable on the left.
Syntax: variable = expression;
Theory of Associativity: Assignment operators have Right-to-Left associativity. This means you can chain them:
a = b = c = 50;
In this case, c becomes 50 first, then b takes the value of c, and finally a takes the value of b.
C++ provides shorthand operators that perform an operation on a variable and then assign the result back to that same variable. These are more than just "shortcuts"; they are often more efficient for the compiler to process.
| Operator | Shorthand | Equivalent to | Theory |
| += | x += y | x = x + y | Add y to x and update x. |
| -= | x -= y | x = x - y | Subtract y from x and update x. |
| *= | x *= y | x = x * y | Multiply x by y and update x. |
| /= | x /= y | x = x / y | Divide x by y and update x. |
| %= | x %= y | x = x % y | Find remainder of x/y and update x. |
C++ also supports assignment for bitwise operations, used frequently in systems programming and embedded systems:
&= (Bitwise AND assignment)
|= (Bitwise OR assignment)
^= (Bitwise XOR assignment)
<<= (Left shift assignment)
>>= (Right shift assignment)
#include <iostream> using namespace std; int main() { int wallet = 100; // Simple assignment int deposit = 50; cout << "Initial wallet: " << wallet << endl; // Compound addition: Adding money wallet += deposit; // wallet = 100 + 50 cout << "After deposit (+=): " << wallet << endl; // Compound subtraction: Spending money wallet -= 30; // wallet = 150 - 30 cout << "After spending (-=): " << wallet << endl; // Compound multiplication: Interest wallet *= 2; // wallet = 120 * 2 cout << "After doubling (*=): " << wallet << endl; // Compound division: Splitting wallet /= 4; // wallet = 240 / 4 cout << "After splitting (/=): " << wallet << endl; // Compound modulo: Finding leftover int items = 10; items %= 3; // items = 10 % 3 (Remainder is 1) cout << "Remaining items (%=): " << items << endl; return 0; }
It is a common misconception that assignment and initialization are the same. In C++ theory, they are distinct:
Initialization: Happens at the moment a variable is created. The memory is filled with a value for the first time.
int x = 10;
Assignment: Happens after a variable already exists. It involves discarding the old value and replacing it with a new one.
x = 20;
Efficiency Note: In advanced C++ (with Objects/Classes), initialization is often significantly faster than assignment because it avoids the overhead of destroying an existing value before creating a new one.
| Feature | Details |
| Operator Type | Binary (requires two operands). |
| Associativity | Right to Left (e.g., x = y = 10). |
| Precedence | Very low (calculated after arithmetic and relational operations). |
| L-value Requirement | The left side must be a modifiable variable. |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION