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 its left operand by its right operand, and subsequently assigns that resulting value back to the left operand.

Syntax and Equivalence

lvalue %= rvalue;
According to the C standard (C11 6.5.16.2), this expression is semantically equivalent to:
lvalue = lvalue % (rvalue);
The parentheses around the rvalue are strictly required in this equivalence due to operator precedence. Without them, an expression like d %= e + 1 would incorrectly expand to d = d % e + 1 (which evaluates as (d % e) + 1), rather than the correct d = d % (e + 1). Additionally, in the compound assignment (%=), the lvalue is evaluated exactly once. This is a critical distinction from the expanded form when the left operand contains side effects (e.g., array[i++] %= 2;).

Technical Constraints

  • Operand Types: Both the left and right operands must be of integral types (e.g., char, short, int, long, long long, or their unsigned variants).
  • Floating-Point Restriction: Applying %= to floating-point types (float, double) violates C language constraints and will result in a compilation error.
  • Modifiable Lvalue: The left operand must be a modifiable lvalue. It cannot be a const-qualified variable or a literal.
  • Associativity: Like all assignment operators in C, %= has right-to-left associativity.

Evaluation Rules and Edge Cases

  • Sign of the Remainder: Under the C99 standard and later, the truncation of integer division is always toward zero. Consequently, the sign of the result of a modulo operation always matches the sign of the dividend (the left operand). The sign of the divisor (the right operand) does not affect the sign of the result.
  • Undefined Behavior:
    • Division by Zero: If the right operand evaluates to 0, the operation triggers undefined behavior, typically manifesting as a hardware exception.
    • Unrepresentable Quotient: If the quotient of the underlying division is not representable, the behavior of both the division (/) and modulo (%) operators is undefined (C11 6.5.5p6). For two’s complement signed integers, this occurs if the minimum representable value of a type is moduloed by -1 (e.g., INT_MIN %= -1). Because the mathematical quotient (INT_MAX + 1) cannot be represented in the signed type, it triggers undefined behavior.

Mechanical Examples

int a = 10;
a %= 3;     // a evaluates to 1 (10 / 3 = 3, remainder 1)

int b = -10;
b %= 3;     // b evaluates to -1 (sign matches the left operand)

int c = 10;
c %= -3;    // c evaluates to 1 (sign matches the left operand)

int d = 5;
int e = 2;
d %= e + 1; // Evaluated as d = d % (e + 1). d becomes 2 (5 % 3)

#include <limits.h>
int f = INT_MIN;
// f %= -1; // WARNING: Undefined behavior (unrepresentable quotient)
// f %= 0;  // WARNING: Undefined behavior (division by zero)
Master C with Deep Grasping Methodology!Learn More