Skip to main content

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.

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.
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.
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.
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.
class BigInt {
public:
    // Member function overload
    BigInt& operator%=(const BigInt& rhs) {
        // Internal modulo logic here
        return *this;
    }
};
Master C++ with Deep Grasping Methodology!Learn More