C++

Assignment Operators in C++

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.

1. The Theory: L-values and R-values

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.

2. Simple Assignment (=)

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.

3. Compound Assignment Operators

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.

OperatorShorthandEquivalent toTheory
+=x += yx = x + yAdd y to x and update x.
-=x -= yx = x - ySubtract y from x and update x.
*=x *= yx = x * yMultiply x by y and update x.
/=x /= yx = x / yDivide x by y and update x.
%=x %= yx = x % yFind remainder of x/y and update x.

Bitwise Compound Assignment (Advanced)

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)

4. Practical Code Example

#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;
}

5. Theoretical Differences: Assignment vs. Initialization

It is a common misconception that assignment and initialization are the same. In C++ theory, they are distinct:

  1. Initialization: Happens at the moment a variable is created. The memory is filled with a value for the first time.

    • int x = 10;

  2. 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.

6. Summary Table for EMBLAb

FeatureDetails
Operator TypeBinary (requires two operands).
AssociativityRight to Left (e.g., x = y = 10).
PrecedenceVery low (calculated after arithmetic and relational operations).
L-value RequirementThe left side must be a modifiable variable.
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now