> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C++ Modulus Assignment

The `%=` (modulo assignment) operator is a compound assignment operator that computes the remainder of the division of a left-hand operand by a right-hand operand, subsequently assigning that computed remainder back to the left-hand operand.

```cpp theme={"dark"}
lhs %= rhs;
```

Semantically, this operation is equivalent to `lhs = lhs % rhs`, with the strict compiler guarantee that the expression `lhs` is evaluated exactly once. This single-evaluation property is critical when `lhs` contains volatile reads or side-effect-producing function calls.

## Type Constraints

The C++ standard enforces strict type requirements for the modulo assignment operator:

* **Integral Types:** The `lhs` must resolve to an integral type (e.g., `int`, `char`, `long`, `short`, `size_t`). The `rhs` may be an integral type or an unscoped enumeration type (which implicitly promotes to an integer). The `lhs` cannot be an enumeration type because the underlying `%` operation yields an integer, and C++ prohibits implicit conversion/assignment from an integer back to an enumeration.
* **Floating-Point Prohibition:** Applying `%=` to `float`, `double`, or `long double` is illegal and will trigger a compilation error.
* **Modifiable Lvalue:** The `lhs` must be a modifiable lvalue. It cannot be `const`-qualified.

## Return Value

The operator returns an lvalue reference to the modified left-hand operand (`lhs`). Because it returns a reference, modulo assignments can be chained, though this is syntactically evaluated from right to left.

```cpp theme={"dark"}
int a = 10;
int b = 3;
a %= b; // a is evaluated to 1, returns reference to a
```

## Precedence and Associativity

* **Precedence:** It shares the lowest precedence level (Level 16) with the standard assignment operator (`=`) and other compound assignment operators (`+=`, `*=`, etc.).
* **Associativity:** Right-to-left. In an expression like `a %= b %= c`, the compiler evaluates `b %= c` first, and the resulting reference of `b` becomes the `rhs` for `a %= b`.

## Sign Rules (C++11 and later)

Following the C++11 standardization of truncated division, the sign of the result assigned to `lhs` is strictly determined by the sign of the initial `lhs` (the dividend). The sign of `rhs` (the divisor) has no effect on the sign of the result.

```cpp theme={"dark"}
int x = -10;
x %= 3;  // x becomes -1
int y = 10;
y %= -3; // y becomes 1
```

## Undefined Behavior

The `%=` operator will invoke undefined behavior (UB) under the following conditions:

* **Division by Zero:** If `rhs` evaluates to `0`, the underlying division cannot be performed, resulting in a hardware trap or unpredictable program execution.
* **Signed Integer Overflow:** According to the C++ standard, if the quotient of `lhs / rhs` is not representable in the result type, the behavior of both the division and modulo operations is undefined. Specifically, if `lhs` holds the minimum representable value for a signed integer type (e.g., `INT_MIN`) and `rhs` is `-1`, the operation `lhs %= -1` invokes undefined behavior and frequently causes a hardware trap on architectures like x86.

## Operator Overloading

For user-defined types (classes and structs), `%=` can be overloaded to define custom modulo assignment semantics. It is conventionally implemented as a member function that modifies the object state and returns `*this` by reference.

```cpp theme={"dark"}
class BigInt {
public:
    // Member function overload
    BigInt& operator%=(const BigInt& rhs) {
        // Internal modulo logic here
        return *this;
    }
};
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C++ Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
